During a machine learning engineer phone screen, the coding task was:
You are given a string text and a list of strings patterns. Return a new string where every substring of text that matches any string in patterns is wrapped in HTML-style bold tags: <b> and </b>.
Rules:
-
If two matched ranges overlap, they must be merged into one bold segment.
-
If two matched ranges are adjacent, they should also be merged into one bold segment.
-
The output should contain the minimum number of bold tag pairs needed to cover all matches.
Example 1:
-
Input:
text = "abcxyz123"
,
patterns = ["abc", "123"]
-
Output:
"<b>abc</b>xyz<b>123</b>"
Example 2:
-
Input:
text = "aaabbcc"
,
patterns = ["aaa", "aab", "bc"]
-
Output:
"<b>aaabbc</b>c"
Implement a function that returns the formatted string.