A newsroom monitoring tool watches a live transcript for a fixed list of alert trigger phrases. Whenever a trigger phrase appears anywhere in the transcript as a contiguous run of characters, every character it covers must be visually emphasized so producers cannot miss it — a phrase may occur more than once, and occurrences of the same or different phrases may overlap or sit right next to each other. Whenever two emphasized character ranges touch or overlap, they must be fused into one unbroken emphasized range rather than shown as two separate adjacent ones. Given the transcript and the list of trigger phrases, print the transcript with each maximal emphasized range wrapped between the markers << (placed immediately before its first character) and >> (placed immediately after its last character); every character that no trigger phrase ever covers is left exactly as it is.
Print a single line: the transcript s with << and >> inserted around every maximal fused emphasized range, with every other character left untouched and in its original order.
Example 1
Input
abcxyz123xyz 2 abc xyz
Expected
<<abcxyz>>123<<xyz>>
Explanation
"abc" matches at index 0-2, and "xyz" matches at index 3-5 and again at index 9-11. The abc match and the first xyz match are adjacent (0-2 then 3-5) so they fuse into one range 0-5, giving "<<abcxyz>>". Indices 6-8 ("123") are untouched. The second xyz match at 9-11 stands alone, giving "<<xyz>>". Output: <<abcxyz>>123<<xyz>>.
Example 2
Input
aaabbb 2 aa ab
Expected
<<aaab>>bb
Explanation
"aa" matches starting at index 0 (covering 0-1) and again starting at index 1 (covering 1-2); "ab" matches starting at index 2 (covering 2-3). These three overlapping/adjacent ranges fuse into one range 0-3, giving "<<aaab>>". The remaining "bb" at indices 4-5 matches no phrase and stays untouched. Output: <<aaab>>bb.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →