-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddBoldTagInString.java
More file actions
27 lines (25 loc) · 843 Bytes
/
AddBoldTagInString.java
File metadata and controls
27 lines (25 loc) · 843 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
public class Solution {
public String addBoldTag(String s, String[] dict) {
boolean[] bold = 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());
}
}
bold[i] = end > i;
}
StringBuilder result = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (!bold[i]) {
result.append(s.charAt(i));
continue;
}
int j = i;
while (j < s.length() && bold[j]) j++;
result.append("<b>" + s.substring(i, j) + "</b>");
i = j - 1;
}
return result.toString();
}
}