Bold Words in String - Problem
Given an array of keywords words and a string s, make all appearances of all keywords words[i] in s bold.
Any letters between <b> and </b> tags become bold.
Return s after adding the bold tags. The returned string should use the least number of tags possible, and the tags should form a valid combination.
Input & Output
Example 1 — Basic Overlapping
$
Input:
words = ["ab", "bc"], s = "abcabc"
›
Output:
"<b>abc</b><b>abc</b>"
💡 Note:
"ab" matches at positions [0,2) and [3,5). "bc" matches at [1,3) and [4,6). Overlapping intervals [0,2) and [1,3) merge to [0,3), and [3,5) and [4,6) merge to [3,6).
Example 2 — No Overlapping
$
Input:
words = ["ab", "cd"], s = "abcd"
›
Output:
"<b>ab</b><b>cd</b>"
💡 Note:
"ab" matches at [0,2) and "cd" matches at [2,4). No overlap, so each gets separate bold tags.
Example 3 — Complete Coverage
$
Input:
words = ["abc"], s = "abc"
›
Output:
"<b>abc</b>"
💡 Note:
Single keyword covers entire string, resulting in one bold region.
Constraints
- 1 ≤ words.length ≤ 1000
- 1 ≤ words[i].length ≤ 1000
- words[i] consists of lowercase English letters only
- 1 ≤ s.length ≤ 1000
- s consists of lowercase English letters only
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of keywords and target string
2
Process
Find all keyword matches and merge overlapping regions
3
Output
String with minimal bold tags around merged regions
Key Takeaway
🎯 Key Insight: Merge overlapping bold regions to use the minimum number of bold tags while covering all keyword matches.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code