We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 896ebfb commit 5bb4915Copy full SHA for 5bb4915
758-bold-words-in-string.js
@@ -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