|
29 | 29 | */
|
30 | 30 | public class _616 {
|
31 | 31 |
|
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 | + } |
39 | 43 | }
|
| 44 | + shouldBold[i] = end > i; |
40 | 45 | }
|
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("" + s.substring(i, j) + ""); |
| 57 | + i = j - 1; |
52 | 58 | }
|
53 |
| - stringBuilder.append("" + s.substring(i, j) + ""); |
54 |
| - i = j - 1; |
| 59 | + return stringBuilder.toString(); |
55 | 60 | }
|
56 |
| - return stringBuilder.toString(); |
57 | 61 | }
|
58 | 62 |
|
59 | 63 | }
|
0 commit comments