|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/** |
4 |
| - * 758. Bold Words in String |
5 |
| - * |
6 |
| - * Given a set of keywords words and a string S, make all appearances of all keywords in S bold. Any letters between <b> and </b> tags become bold. |
7 |
| - * The returned string should use the least number of tags possible, and of course the tags should form a valid combination. |
8 |
| - * For example, given that words = ["ab", "bc"] and S = "aabcd", we should return "a<b>abc</b>d". |
9 |
| - * Note that returning "a<b>a<b>b</b>c</b>d" would use more tags, so it is incorrect. |
10 |
| -
|
11 |
| - Note: |
12 |
| -
|
13 |
| - words has length in range [0, 50]. |
14 |
| - words[i] has length in range [1, 10]. |
15 |
| - S has length in range [0, 500]. |
16 |
| - All characters in words[i] and S are lowercase letters. |
17 |
| -
|
18 |
| - */ |
19 |
| - |
20 | 3 | public class _758 {
|
21 |
| - public static class Solution1 { |
22 |
| - /**Interestingly, this problem is exactly the same as 616, using 616's code could get it AC'ed.*/ |
23 |
| - public String boldWords(String[] words, String S) { |
24 |
| - boolean[] shouldBold = new boolean[S.length()]; |
25 |
| - for (int i = 0, end = 0; i < S.length(); i++) { |
26 |
| - for (String word : words) { |
27 |
| - if (S.startsWith(word, i)) { |
28 |
| - end = Math.max(end, i + word.length()); |
29 |
| - } |
30 |
| - } |
31 |
| - shouldBold[i] = end > i; |
32 |
| - } |
33 |
| - StringBuilder stringBuilder = new StringBuilder(); |
34 |
| - for (int i = 0; i < S.length(); i++) { |
35 |
| - if (!shouldBold[i]) { |
36 |
| - stringBuilder.append(S.charAt(i)); |
37 |
| - continue; |
38 |
| - } |
39 |
| - int j = i; |
40 |
| - while (j < S.length() && shouldBold[j]) { |
41 |
| - j++; |
| 4 | + public static class Solution1 { |
| 5 | + /** |
| 6 | + * Interestingly, this problem is exactly the same as 616, using 616's code could get it AC'ed. |
| 7 | + */ |
| 8 | + public String boldWords(String[] words, String S) { |
| 9 | + boolean[] shouldBold = new boolean[S.length()]; |
| 10 | + for (int i = 0, end = 0; i < S.length(); i++) { |
| 11 | + for (String word : words) { |
| 12 | + if (S.startsWith(word, i)) { |
| 13 | + end = Math.max(end, i + word.length()); |
| 14 | + } |
| 15 | + } |
| 16 | + shouldBold[i] = end > i; |
| 17 | + } |
| 18 | + StringBuilder stringBuilder = new StringBuilder(); |
| 19 | + for (int i = 0; i < S.length(); i++) { |
| 20 | + if (!shouldBold[i]) { |
| 21 | + stringBuilder.append(S.charAt(i)); |
| 22 | + continue; |
| 23 | + } |
| 24 | + int j = i; |
| 25 | + while (j < S.length() && shouldBold[j]) { |
| 26 | + j++; |
| 27 | + } |
| 28 | + stringBuilder.append("<b>" + S.substring(i, j) + "</b>"); |
| 29 | + i = j - 1; |
| 30 | + } |
| 31 | + return stringBuilder.toString(); |
42 | 32 | }
|
43 |
| - stringBuilder.append("<b>" + S.substring(i, j) + "</b>"); |
44 |
| - i = j - 1; |
45 |
| - } |
46 |
| - return stringBuilder.toString(); |
47 | 33 | }
|
48 |
| - } |
49 | 34 | }
|
0 commit comments