-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0758-bold-words-in-string.js
More file actions
57 lines (48 loc) · 1.54 KB
/
0758-bold-words-in-string.js
File metadata and controls
57 lines (48 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* 758. Bold Words in String
* https://leetcode.com/problems/bold-words-in-string/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {string[]} words
* @param {string} s
* @return {string}
*/
var boldWords = function(words, s) {
const boldIntervals = [];
for (const word of words) {
let start = s.indexOf(word);
while (start !== -1) {
boldIntervals.push([start, start + word.length]);
start = s.indexOf(word, start + 1);
}
}
if (!boldIntervals.length) return s;
boldIntervals.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
const mergedIntervals = [];
let [currentStart, currentEnd] = boldIntervals[0];
for (let i = 1; i < boldIntervals.length; i++) {
const [nextStart, nextEnd] = boldIntervals[i];
if (nextStart <= currentEnd) {
currentEnd = Math.max(currentEnd, nextEnd);
} else {
mergedIntervals.push([currentStart, currentEnd]);
[currentStart, currentEnd] = [nextStart, nextEnd];
}
}
mergedIntervals.push([currentStart, currentEnd]);
let result = '';
let lastEnd = 0;
for (const [start, end] of mergedIntervals) {
result += s.slice(lastEnd, start) + '<b>' + s.slice(start, end) + '</b>';
lastEnd = end;
}
result += s.slice(lastEnd);
return result;
};