Skip to content

Commit 8649208

Browse files
add 758
1 parent 512de10 commit 8649208

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Your ideas/fixes/algorithms are more than welcome!
2727
|763|[Partition Labels](https://leetcode.com/problems/partition-labels/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_763.java) | O(n) | O(1) | |Medium|
2828
|762|[Prime Number of Set Bits in Binary Representation](https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_762.java) | O(n) | O(1) | |Easy|
2929
|760|[Find Anagram Mappings](https://leetcode.com/problems/find-anagram-mappings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_760.java) | O(n^2) | O(1) | |Easy|
30+
|758|[Bold Words in String](https://leetcode.com/problems/bold-words-in-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_758.java) | O(n*k) | O(n | |Easy|
3031
|756|[Pyramid Transition Matrix](https://leetcode.com/problems/pyramid-transition-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_756.java) | O(?) | O(?) | |Medium| Backtracking
3132
|755|[Pour Water](https://leetcode.com/problems/pour-water/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_755.java) | O(V*N) | O(1) | |Medium| Array
3233
|754|[Reach a Number](https://leetcode.com/problems/reach-a-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_754.java) | O(n) | O(1) | |Medium| Math
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.fishercoder.solutions;
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+
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++;
42+
}
43+
stringBuilder.append("<b>" + S.substring(i, j) + "</b>");
44+
i = j - 1;
45+
}
46+
return stringBuilder.toString();
47+
}
48+
}
49+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._758;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _758Test {
10+
private static _758.Solution1 solution1;
11+
private static String[] words;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _758.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
words = new String[] {"ab", "bc"};
21+
assertEquals("a<b>abc</b>d", solution1.boldWords(words, "aabcd"));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
words = new String[] {"ccb", "b", "d", "cba", "dc"};
27+
assertEquals("eeaa<b>d</b>a<b>d</b>a<b>dc</b>", solution1.boldWords(words, "eeaadadadc"));
28+
}
29+
}

0 commit comments

Comments
 (0)