Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3574b2f

Browse files
refactor 139
1 parent 5757623 commit 3574b2f

File tree

2 files changed

+19
-54
lines changed

2 files changed

+19
-54
lines changed

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

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,40 @@
11
package com.fishercoder.solutions;
22

3-
import java.util.HashSet;
3+
import com.fishercoder.common.utils.CommonUtils;
4+
45
import java.util.List;
5-
import java.util.Set;
66

77
public class _139 {
88

99
public static class Solution1 {
10-
public boolean wordBreak(String s, List<String> wordDict) {
11-
return wordBreak(s, new HashSet(wordDict), 0);
12-
}
13-
14-
public boolean wordBreak(String s, Set<String> wordDict, int start) {
15-
if (start == s.length()) {
16-
return true;
17-
}
18-
for (int end = start + 1; end <= s.length(); end++) {
19-
if (wordDict.contains(s.substring(start, end)) && wordBreak(s, wordDict, end)) {
20-
return true;
21-
}
22-
}
23-
return false;
24-
}
25-
}
26-
27-
public static class Solution2 {
2810
/**
29-
* this beats 70.46% submission.
11+
* this solution takes between 7 and 8 ms to finish on LeetCode
12+
* beats around 38% to 48% submissions as of 6/27/2020
3013
*/
3114
public boolean wordBreak(String s, List<String> wordDict) {
3215
int n = s.length();
3316
boolean[] dp = new boolean[n + 1];
3417
dp[0] = true;
3518
for (int i = 1; i <= n; i++) {
3619
for (int j = 0; j < i; j++) {
37-
if (dp[j] && wordDict.contains(s.substring(j, i))) {
20+
if (dp[j]
21+
&&
22+
wordDict.contains(s.substring(j, i))) {
3823
dp[i] = true;
3924
break;
4025
}
4126
}
4227
}
28+
CommonUtils.printArray(dp);
4329
return dp[n];
4430
}
4531
}
4632

47-
public static class Solution3 {
33+
public static class Solution2 {
4834
/**
49-
* Added pruning.
50-
* this beats 89.91% submissions.
35+
* Added pruning based on max word length.
36+
* this solution takes between 2 and 3 ms to finish on LeetCode
37+
* this beats 94.53% submissions as of 6/27/2020
5138
*/
5239
public boolean wordBreak(String s, List<String> wordDict) {
5340
int maxLen = Integer.MIN_VALUE;
@@ -73,10 +60,11 @@ public boolean wordBreak(String s, List<String> wordDict) {
7360
}
7461
}
7562

76-
public static class Solution4 {
63+
public static class Solution3 {
7764
/**
7865
* Added pruning, plus start from the end to check.
79-
* This beats 95.20% submissions.
66+
* This solution takes 1 ms to finish on LeetCode
67+
* This beats 99.02% submissions as of 6/27/2020.
8068
*/
8169
public boolean wordBreak(String s, List<String> wordDict) {
8270
int maxLen = Integer.MIN_VALUE;
@@ -88,13 +76,8 @@ public boolean wordBreak(String s, List<String> wordDict) {
8876
boolean[] dp = new boolean[n + 1];
8977
dp[0] = true;
9078
for (int i = 1; i <= n; i++) {
91-
for (int lastWordLength = 1; lastWordLength <= i && lastWordLength <= maxLen;
92-
lastWordLength++) {
93-
if (!dp[i - lastWordLength]) {
94-
continue;
95-
}
96-
String sub = s.substring(i - lastWordLength, i);
97-
if (wordDict.contains(sub)) {
79+
for (int lastWordLength = 1; lastWordLength <= i && lastWordLength <= maxLen; lastWordLength++) {
80+
if (dp[i - lastWordLength] && wordDict.contains(s.substring(i - lastWordLength, i))) {
9881
dp[i] = true;
9982
break;
10083
}

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public class _139Test {
1515
private static _139.Solution1 solution1;
1616
private static _139.Solution2 solution2;
1717
private static _139.Solution3 solution3;
18-
private static _139.Solution4 solution4;
1918
private static String s;
2019
private static List<String> wordDict;
2120

@@ -24,7 +23,6 @@ public static void setup() {
2423
solution1 = new _139.Solution1();
2524
solution2 = new _139.Solution2();
2625
solution3 = new _139.Solution3();
27-
solution4 = new _139.Solution4();
2826
}
2927

3028
@Test
@@ -50,39 +48,23 @@ public void test3() {
5048

5149
@Test
5250
public void test4() {
53-
s = "leetcode";
54-
wordDict = new ArrayList<>(Arrays.asList("leet", "code"));
55-
assertEquals(true, solution4.wordBreak(s, wordDict));
56-
}
57-
58-
@Test
59-
@Ignore
60-
public void test5() {
61-
// this one will time out due to the inefficient algorithm, so ignore it
6251
s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab";
6352
wordDict = new ArrayList<>(Arrays.asList("a", "aa", "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa", "aaaaaaaa", "aaaaaaaaa", "aaaaaaaaaa"));
6453
assertEquals(false, solution1.wordBreak(s, wordDict));
6554
}
6655

6756
@Test
68-
public void test6() {
57+
public void test5() {
6958
s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab";
7059
wordDict = new ArrayList<>(Arrays.asList("a", "aa", "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa", "aaaaaaaa", "aaaaaaaaa", "aaaaaaaaaa"));
7160
assertEquals(false, solution2.wordBreak(s, wordDict));
7261
}
7362

7463
@Test
75-
public void test7() {
64+
public void test6() {
7665
s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab";
7766
wordDict = new ArrayList<>(Arrays.asList("a", "aa", "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa", "aaaaaaaa", "aaaaaaaaa", "aaaaaaaaaa"));
7867
assertEquals(false, solution3.wordBreak(s, wordDict));
7968
}
8069

81-
@Test
82-
public void test8() {
83-
s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab";
84-
wordDict = new ArrayList<>(Arrays.asList("a", "aa", "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa", "aaaaaaaa", "aaaaaaaaa", "aaaaaaaaaa"));
85-
assertEquals(false, solution4.wordBreak(s, wordDict));
86-
}
87-
8870
}

0 commit comments

Comments
 (0)