The Bold Words in String interview question asks you to wrap specific substrings within <b> and </b> tags. You are given a list of "words" to search for and a target "string". If two bold substrings overlap or are adjacent, they should be merged into a single set of tags. This is a classic string manipulation and pattern-matching problem.
Google frequently asks this to test a candidate's string handling and ability to manage intervals. It requires multiple steps: finding all occurrences of words, marking which characters should be bold, and then formatting the output. It tests whether you can break a problem into logical sub-tasks and handle overlapping ranges correctly.
This problem can be solved using String Matching (like Aho-Corasick or simple indexOf) and a boolean mask. You create a boolean array is_bold of the same length as the string. For each word in your list, find all its occurrences in the string and mark the corresponding indices in is_bold as true. Finally, iterate through the string and the boolean array to insert <b> tags whenever is_bold switches from false to true, and </b> when it switches from true to false.
Words: ["ab", "bc"], String: "aabcd"
is_bold[1...2] = true.is_bold[2...3] = true.a + <b> + abc + </b> + d -> a<b>abc</b>d.One common mistake is trying to insert the tags immediately while finding words, which makes handling overlaps extremely difficult. Another is failing to handle the case where words are adjacent but not overlapping (e.g., "abc" and "def" in "abcdef"). Using a boolean mask or an interval-merging approach is much cleaner and less error-prone.
For any "marking" or "tagging" problem on a string or array, think about using a secondary boolean array or a set of intervals. Separating the "identification" phase from the "output" phase is a great way to simplify your logic.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Add Bold Tag in String | Medium | Solve | |
| Replace Words | Medium | Solve | |
| Find the Length of the Longest Common Prefix | Medium | Solve | |
| Short Encoding of Words | Medium | Solve | |
| Shortest Uncommon Substring in an Array | Medium | Solve |