Skip to content

Commit 4761bea

Browse files
refactor 139
1 parent d4bba93 commit 4761bea

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.fishercoder.solutions;
22

33
import java.util.List;
4-
import java.util.Set;
54

65
/**
76
* 139. Word Break
@@ -24,7 +23,7 @@ The wordDict parameter had been changed to a list of strings (instead of a set o
2423
public class _139 {
2524

2625
public static class Solution1 {
27-
/**this beats 70.46% submission. */
26+
/** this beats 70.46% submission. */
2827
public boolean wordBreak(String s, List<String> wordDict) {
2928
int n = s.length();
3029
boolean[] dp = new boolean[n + 1];
@@ -75,7 +74,7 @@ public static class Solution3 {
7574
* Added pruning, plus start from the end to check.
7675
* This beats 95.20% submissions.
7776
*/
78-
public boolean wordBreak(String s, Set<String> wordDict) {
77+
public boolean wordBreak(String s, List<String> wordDict) {
7978
int maxLen = Integer.MIN_VALUE;
8079
for (String word : wordDict) {
8180
maxLen = (word.length() > maxLen) ? word.length() : maxLen;
@@ -85,7 +84,8 @@ public boolean wordBreak(String s, Set<String> wordDict) {
8584
boolean[] dp = new boolean[n + 1];
8685
dp[0] = true;
8786
for (int i = 1; i <= n; i++) {
88-
for (int lastWordLength = 1; lastWordLength <= i && lastWordLength <= maxLen; lastWordLength++) {
87+
for (int lastWordLength = 1; lastWordLength <= i && lastWordLength <= maxLen;
88+
lastWordLength++) {
8989
if (!dp[i - lastWordLength]) {
9090
continue;
9191
}

src/test/java/com/fishercoder/_139Test.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,25 @@
1111
import static junit.framework.Assert.assertEquals;
1212

1313
public class _139Test {
14-
private static _139.Solution2 solution2;
15-
private static String s;
16-
private static List<String> wordDict;
14+
private static _139.Solution1 solution1;
15+
private static _139.Solution2 solution2;
16+
private static _139.Solution3 solution3;
17+
private static String s;
18+
private static List<String> wordDict;
1719

18-
@BeforeClass
19-
public static void setup() {
20-
solution2 = new _139.Solution2();
21-
}
20+
@BeforeClass
21+
public static void setup() {
22+
solution1 = new _139.Solution1();
23+
solution2 = new _139.Solution2();
24+
solution3 = new _139.Solution3();
25+
}
2226

23-
@Test
24-
public void test1() {
25-
s = "leetcode";
26-
wordDict = new ArrayList<>(Arrays.asList("leet", "code"));
27-
assertEquals(true, solution2.wordBreak(s, wordDict));
28-
}
27+
@Test
28+
public void test1() {
29+
s = "leetcode";
30+
wordDict = new ArrayList<>(Arrays.asList("leet", "code"));
31+
assertEquals(true, solution1.wordBreak(s, wordDict));
32+
assertEquals(true, solution2.wordBreak(s, wordDict));
33+
assertEquals(true, solution3.wordBreak(s, wordDict));
34+
}
2935
}

0 commit comments

Comments
 (0)