Skip to content

Commit 4ed4db9

Browse files
refactor 616
1 parent 05b4419 commit 4ed4db9

File tree

1 file changed

+25
-21
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+25
-21
lines changed

src/main/java/com/fishercoder/solutions/_616.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,35 @@
2929
*/
3030
public class _616 {
3131

32-
/**credit: https://discuss.leetcode.com/topic/92112/java-solution-boolean-array*/
33-
public String addBoldTag(String s, String[] dict) {
34-
boolean[] shouldBold = new boolean[s.length()];
35-
for (int i = 0, end = 0; i < s.length(); i++) {
36-
for (String word : dict) {
37-
if (s.startsWith(word, i)) {
38-
end = Math.max(end, i + word.length());
32+
public static class Solution1 {
33+
/**
34+
* credit: https://discuss.leetcode.com/topic/92112/java-solution-boolean-array
35+
*/
36+
public String addBoldTag(String s, String[] dict) {
37+
boolean[] shouldBold = new boolean[s.length()];
38+
for (int i = 0, end = 0; i < s.length(); i++) {
39+
for (String word : dict) {
40+
if (s.startsWith(word, i)) {
41+
end = Math.max(end, i + word.length());
42+
}
3943
}
44+
shouldBold[i] = end > i;
4045
}
41-
shouldBold[i] = end > i;
42-
}
43-
StringBuilder stringBuilder = new StringBuilder();
44-
for (int i = 0; i < s.length(); i++) {
45-
if (!shouldBold[i]) {
46-
stringBuilder.append(s.charAt(i));
47-
continue;
48-
}
49-
int j = i;
50-
while (j < s.length() && shouldBold[j]) {
51-
j++;
46+
StringBuilder stringBuilder = new StringBuilder();
47+
for (int i = 0; i < s.length(); i++) {
48+
if (!shouldBold[i]) {
49+
stringBuilder.append(s.charAt(i));
50+
continue;
51+
}
52+
int j = i;
53+
while (j < s.length() && shouldBold[j]) {
54+
j++;
55+
}
56+
stringBuilder.append("<b>" + s.substring(i, j) + "</b>");
57+
i = j - 1;
5258
}
53-
stringBuilder.append("<b>" + s.substring(i, j) + "</b>");
54-
i = j - 1;
59+
return stringBuilder.toString();
5560
}
56-
return stringBuilder.toString();
5761
}
5862

5963
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy