Skip to content

Latest commit

 

History

History
92 lines (68 loc) · 2.4 KB

_616. Add Bold Tag in String.md

File metadata and controls

92 lines (68 loc) · 2.4 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 28, 2024

Last updated : June 28, 2024


Related Topics : Array, Hash Table, String, Trie, String Matching

Acceptance Rate : 50.09 %


Solutions

Python

# Identical to 758

class Solution:
    def addBoldTag(self, s: str, words: List[str]) -> str:
        trie = {}

        for word in words :
            curr = trie
            for c in word :
                if c not in curr :
                    curr[c] = {}
                curr = curr[c]

            curr[False] = True
        
        # Find intervals that need to be bolded
        intervals = []
        curr = {}
        for i, c in enumerate(s) :
            curr[i] = trie
            keys = list(curr.keys())
            for key in keys :
                if False in curr[key] :
                    intervals.append((key, i))
                
                if c not in curr[key] :
                    curr.pop(key)
                    continue
                
                curr[key] = curr[key][c]

        for i, branch in curr.items() :
            if False in branch :
                intervals.append((i, len(s)))

        intervals.sort(key=lambda x: x[0])

        # merging intervals
        output = []
        if intervals :
            output.append(s[:intervals[0][0]])
        
        indx = 0
        while indx < len(intervals) :
            left, right = intervals[indx]
            indx += 1

            while indx < len(intervals) and intervals[indx][0] <= right :
                if intervals[indx][1] > right :
                    right = intervals[indx][1]
                indx += 1

            output.append('<b>')
            output.append(s[left:right])
            output.append('</b>')
            
            if indx < len(intervals) :
                output.append(s[right:intervals[indx][0]])
        if intervals :
            output.append(s[right:])
        else :
            output.append(s)

        return ''.join(output)