{x}
blog image

Bold Words in String

Solution Explanation:

This problem aims to add bold tags (<b> and </b>) around all occurrences of keywords from a given list within a string, using the minimum number of tags. The solution uses a Trie data structure for efficient keyword searching.

Approach:

  1. Trie Construction: A Trie is built from the given list of keywords. A Trie allows for quick prefix matching, making it efficient to search for keyword occurrences within the string.

  2. Keyword Searching: The code iterates through the string. For each character, it traverses the Trie, checking if a keyword is present starting at that character. If a keyword is found, the starting and ending indices of the keyword are stored.

  3. Merging Overlapping Bold Ranges: The algorithm identifies all the ranges where bold tags need to be applied. It then merges overlapping or consecutive ranges to minimize the number of <b> and </b> tags needed.

  4. Bold Tag Insertion: Finally, the code iterates through the string and inserts the <b> and </b> tags at the appropriate locations based on the merged ranges.

Time Complexity Analysis:

  • Trie Construction: O(W), where W is the total number of characters in all keywords. Inserting each word into the trie takes time proportional to its length.

  • Keyword Searching: O(S*K), where S is the length of the string and K is the average length of a keyword. In the worst case, the algorithm might check every possible substring of length K.

  • Merging Overlapping Ranges: O(R log R) where R is the number of ranges found; it can be at most O(S). This is due to sorting the ranges (if necessary, depending on the merging algorithm), which typically has O(n log n) complexity.

  • Bold Tag Insertion: O(S) because we iterate through the string once.

Therefore, the overall time complexity is dominated by the keyword search and potentially the range merging, resulting in O(S*K + S log S) in the worst case. If the number of ranges (R) is significantly smaller than S, the time complexity could be closer to O(S*K).

Space Complexity Analysis:

  • Trie: The space used by the Trie is proportional to the total number of characters in all keywords, so it's O(W).

  • Ranges: The space used to store the ranges is proportional to the length of the string in the worst case (when every character is part of a keyword), leading to O(S).

The overall space complexity is O(S + W).

Code Explanation (Python):

The Python code implements the Trie using a class. The insert method adds keywords to the Trie. The boldWords method performs the keyword search, range merging, and bold tag insertion. The merging of overlapping ranges is done iteratively by considering ranges in order and extending the end index of a range if an overlapping range is found.

Code Explanation (Other Languages):

The Java, C++, and Go codes follow the same logic and algorithmic steps as the Python code, with the Trie structure and other details adapted to each language's syntax and conventions. They all aim for the same time and space complexity as described above.