forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_616.java
36 lines (33 loc) · 1.17 KB
/
_616.java
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
package com.fishercoder.solutions;
public class _616 {
public static class Solution1 {
/**
* credit: https://discuss.leetcode.com/topic/92112/java-solution-boolean-array
*/
public String addBoldTag(String s, String[] dict) {
boolean[] shouldBold = new boolean[s.length()];
for (int i = 0, end = 0; i < s.length(); i++) {
for (String word : dict) {
if (s.startsWith(word, i)) {
end = Math.max(end, i + word.length());
}
}
shouldBold[i] = end > i;
}
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (!shouldBold[i]) {
stringBuilder.append(s.charAt(i));
continue;
}
int j = i;
while (j < s.length() && shouldBold[j]) {
j++;
}
stringBuilder.append("<b>" + s.substring(i, j) + "</b>");
i = j - 1;
}
return stringBuilder.toString();
}
}
}