Skip to content

Commit 5bb4915

Browse files
authored
Create 758-bold-words-in-string.js
1 parent 896ebfb commit 5bb4915

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

758-bold-words-in-string.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {string[]} words
3+
* @param {string} S
4+
* @return {string}
5+
*/
6+
const boldWords = function (words, S) {
7+
const boldMap = new Array(S.length).fill(0)
8+
for (let i = 0; i < words.length; i++) {
9+
let match = -1
10+
while ((match = S.indexOf(words[i], match + 1)) > -1) {
11+
for (let j = match; j < match + words[i].length; j++) {
12+
boldMap[j] = 1
13+
}
14+
}
15+
}
16+
let res = ''
17+
let openTag = false
18+
for (let i = 0; i < S.length; i++) {
19+
if (boldMap[i] && !openTag) {
20+
res += `<b>`
21+
openTag = true
22+
} else if (!boldMap[i] && openTag) {
23+
res += `</b>`
24+
openTag = false
25+
}
26+
res += S[i]
27+
}
28+
res += openTag ? `</b>` : ``
29+
return res
30+
}

0 commit comments

Comments
 (0)