In the Add Bold Tag in String coding problem, you are given a main string and a list of dictionary words. You must find all occurrences of these dictionary words within the main string and wrap them in <b> and </b> tags. If two bolded regions overlap or are adjacent, they should be merged into a single tag.
This is a popular Medium difficulty question at Google and Microsoft because it combines string matching with interval merging. It tests your ability to transform a set of distinct "hits" into a clean, formatted output, demonstrating proficiency in both search algorithms and data processing.
This problem effectively uses the String Matching interview pattern combined with Interval Merging.
isBold of the same length as the main string.isBold indices as true.isBold array to determine where to place the opening and closing tags.String: "aabbcc", Dictionary: ["aabb", "bbcc"]
[0, 1, 2, 3] as bold.[2, 3, 4, 5] as bold.isBold array is true for all indices 0 through 5.<b>aabbcc</b> (one merged tag instead of <b>aabb</b><b>bbcc</b>).<b>a</b><b>b</b> should be <b>ab</b>).Whenever you have overlapping ranges (like bold indices), a boolean mask (an array of true/false) is often easier to manage than a list of start/end pairs.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Bold Words 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 |