Italicize Matching Word Regions
Problem statement
Given a larger string text and a list of non-empty words, surround every character belonging to a literal word occurrence with italic XML tags <i> and </i>. Matches are case-sensitive and may overlap.
Merge overlapping or immediately adjacent matching ranges into one region, so the returned string contains no redundant inner tag pairs. Preserve all original characters and their order. Words may repeat, and either the text or the word list may be empty.
Function
italicizeWordMatches(text: String, words: String[]) → StringExamples
Example 1
text = "abcxyz123"words = ["abc","123"]return = "<i>abc</i>xyz<i>123</i>"Two disjoint matching regions remain separate.
Example 2
text = "aaabbcc"words = ["aaa","aab","bc"]return = "<i>aaabbc</i>c"Overlapping matches extend one connected region.
Example 3
text = "abcdef"words = ["ab","cd","ef"]return = "<i>abcdef</i>"Adjacent matching regions combine without inner tag pairs.
Constraints
0 <= text.length <= 2000and0 <= words.length <= 100.- Every word contains between
1and50characters. - Text and words contain ASCII letters, digits and spaces; they contain no XML-special characters.