-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
41 lines (29 loc) · 1001 Bytes
/
Copy pathSolution.java
File metadata and controls
41 lines (29 loc) · 1001 Bytes
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
class Solution {
private static final String START_BOLD = "<b>";
private static final String END_BOLD = "</b>";
public String addBoldTag(String s, String[] words) {
int n = s.length();
char[] arr = s.toCharArray();
boolean[] isBold = new boolean[n];
for (String word : words) {
int start = s.indexOf(word);
while (start != -1) {
for (int i = start; i < start + word.length(); i++) {
isBold[i] = true;
}
start = s.indexOf(word, start + 1);
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
if (isBold[i] && (i == 0 || !isBold[i - 1])) {
sb.append(START_BOLD);
}
sb.append(arr[i]);
if (isBold[i] && (i == n - 1 || !isBold[i + 1])) {
sb.append(END_BOLD);
}
}
return sb.toString();
}
}