FastPrepItalicize Matching Word Regions

Italicize Matching Word Regions

Microsoft logoMicrosoft● MediumFULLTIMEONSITE INTERVIEW
Learn

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[]) → String

Examples

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 <= 2000 and 0 <= words.length <= 100.
  • Every word contains between 1 and 50 characters.
  • Text and words contain ASCII letters, digits and spaces; they contain no XML-special characters.

More Microsoft problems

See Microsoft hiring insights
public String italicizeWordMatches(String text, String[] words) {
    // Write your code here
}
text"abcxyz123"
words["abc","123"]
expected"<i>abc</i>xyz<i>123</i>"
Checking account…