Add Bold Tag in String

MediumStringshashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given a string s and a list of keywords words. Every character of s that lies inside **any** occurrence of any keyword should be bolded. Occurrences may overlap. Wrap each maximal run of bolded characters in a single pair of tags <b> and </b>. Two bold stretches that overlap **or touch** (one ends right where the next begins) form one run and get one pair of tags. Return the resulting string. If nothing is bolded, return s unchanged.

Examples

Input: s = "abcxyz123", words = ["abc","123"]
Output: "<b>abc</b>xyz<b>123</b>"
The two keyword matches are separated by `xyz`, so they get separate tags.
Input: s = "aaabbb", words = ["aa","b"]
Output: "<b>aaabbb</b>"
`"aa"` matches at 0 and 1, covering `aaa`; each `b` is bolded and touches the previous stretch, so the whole string is one run.

Constraints

  • `1 <= s.length <= 1000`
  • `0 <= words.length <= 100`
  • `1 <= words[i].length <= 1000`
  • `s` and `words[i]` contain only English letters and digits
  • All values in `words` are distinct

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems