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

Commit 3877930

Browse files
refactor 139
1 parent d03c333 commit 3877930

File tree

3 files changed

+102
-42
lines changed

3 files changed

+102
-42
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ Your ideas/fixes/algorithms are more than welcome!
458458
|142|[Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_142.java)| O(n)|O(1) | Medium| Linked List
459459
|141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_141.java)| O(n)|O(1) | Easy| Linked List
460460
|140|[Word Break II](https://leetcode.com/problems/word-break-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_140.java)| ? |O(n^2) | Hard| Backtracking/DFS
461-
|139|[Word Break](https://leetcode.com/problems/word-break/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_139.java)| O(n^2)|O(n) | Medium| DP
461+
|139|[Word Break](https://leetcode.com/problems/word-break/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_139.java)| O(n^2)|O(n) | Medium| DP, Pruning
462462
|138|[Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_138.java)| O(n)|O(n) | Medium| LinkedList, HashMap
463463
|137|[Single Number II](https://leetcode.com/problems/single-number-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_137.java)| O(n)|O(n) | Medium|
464464
|136|[Single Number](https://leetcode.com/problems/single-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_136.java)| O(n)|O(n) | Medium|
Lines changed: 72 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.List;
34
import java.util.Set;
45

5-
/**Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.
6+
/**
7+
* 139. Word Break
8+
* Given a non-empty string s and a dictionary wordDict containing a list of non-empty words,
9+
* determine if s can be segmented into a space-separated sequence of one or more dictionary words.
10+
* You may assume the dictionary does not contain duplicate words.
611
712
For example, given
813
s = "leetcode",
@@ -11,57 +16,83 @@
1116
Return true because "leetcode" can be segmented as "leet code".
1217
1318
UPDATE (2017/1/4):
14-
The wordDict parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.*/
15-
public class _139 {
19+
The wordDict parameter had been changed to a list of strings (instead of a set of strings).
20+
Please reload the code definition to get the latest changes.
21+
*/
1622

17-
//Jiuzhang gives a very good illustration for this problem!
23+
public class _139 {
1824

19-
public boolean wordBreak_2ms(String s, Set<String> wordDict) {
20-
int maxLen = Integer.MIN_VALUE;
21-
for(String word : wordDict){
22-
maxLen = (word.length() > maxLen) ? word.length() : maxLen;
23-
}
24-
25-
int n = s.length();
26-
boolean[] dp = new boolean[n+1];
27-
dp[0] = true;
28-
for(int i = 1; i <= n; i++){
29-
for(int lastWordLength = 1; lastWordLength <= i && lastWordLength <= maxLen; lastWordLength++){
30-
if(!dp[i-lastWordLength]) continue;
31-
32-
String sub = s.substring(i-lastWordLength, i);
33-
if(wordDict.contains(sub)){
34-
dp[i] = true;
35-
break;
25+
public static class PureDPSolution {
26+
/**
27+
* This beats 70.10% submissions.
28+
*/
29+
public boolean wordBreak(String s, List<String> wordDict) {
30+
int n = s.length();
31+
boolean[] dp = new boolean[n + 1];
32+
dp[0] = true;
33+
for (int i = 1; i <= n; i++) {
34+
for (int j = 0; j < i; j++) {
35+
if (dp[j] && wordDict.contains(s.substring(j, i))) {
36+
dp[i] = true;
37+
break;
38+
}
3639
}
3740
}
41+
return dp[n];
3842
}
39-
40-
return dp[n];
4143
}
42-
4344

44-
//this is much slower, although AC'ed on Leetcode, TLE on Lintcode.
45-
//This is because in the inner for loop, this method is looping from left to right which is unnecessary, we only need to find the
46-
//right-most true element, then check that substring. That's why we could write wordBreak_2ms() above.
47-
public boolean wordBreak_14ms(String s, Set<String> wordDict) {
48-
int n = s.length();
49-
boolean[] dp = new boolean[n+1];
50-
dp[0] = true;
51-
for(int i = 1; i <= n; i++){
52-
for(int j = 0; j < i; j++){
53-
if(!dp[j]) continue;
54-
55-
String sub = s.substring(j, i);
56-
if(wordDict.contains(sub)){
57-
dp[i] = true;
58-
break;
45+
public static class ModifiedDPAndPruningSolution {
46+
/**
47+
* This beats 86.09% submissions.
48+
*/
49+
public boolean wordBreak(String s, List<String> wordDict) {
50+
int maxLen = Integer.MIN_VALUE;
51+
for (String word : wordDict) {
52+
maxLen = (word.length() > maxLen) ? word.length() : maxLen;
53+
}
54+
55+
int n = s.length();
56+
boolean[] dp = new boolean[n + 1];
57+
dp[0] = true;
58+
for (int i = 1; i <= n; i++) {
59+
for (int j = 0; j < i; j++) {
60+
if ((i - j) > maxLen) continue;
61+
if (dp[j] && wordDict.contains(s.substring(j, i))) {
62+
dp[i] = true;
63+
break;
64+
}
5965
}
6066
}
67+
return dp[n];
6168
}
62-
63-
return dp[n];
6469
}
6570

71+
public static class DPAndPruningSolution {
72+
/**
73+
* This beats 97.08% submissions.
74+
*/
75+
public boolean wordBreak(String s, Set<String> wordDict) {
76+
int maxLen = Integer.MIN_VALUE;
77+
for (String word : wordDict) {
78+
maxLen = (word.length() > maxLen) ? word.length() : maxLen;
79+
}
80+
81+
int n = s.length();
82+
boolean[] dp = new boolean[n + 1];
83+
dp[0] = true;
84+
for (int i = 1; i <= n; i++) {
85+
for (int lastWordLength = 1; lastWordLength <= i && lastWordLength <= maxLen; lastWordLength++) {
86+
if (!dp[i - lastWordLength]) continue;
87+
String sub = s.substring(i - lastWordLength, i);
88+
if (wordDict.contains(sub)) {
89+
dp[i] = true;
90+
break;
91+
}
92+
}
93+
}
94+
return dp[n];
95+
}
96+
}
6697

6798
}
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._139;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
import static junit.framework.Assert.assertEquals;
12+
13+
public class _139Test {
14+
private static _139.ModifiedDPAndPruningSolution modifiedDpAndPruningSolution;
15+
private static String s;
16+
private static List<String> wordDict;
17+
18+
@BeforeClass
19+
public static void setup(){
20+
modifiedDpAndPruningSolution = new _139.ModifiedDPAndPruningSolution();
21+
}
22+
23+
@Test
24+
public void test1(){
25+
s = "leetcode";
26+
wordDict = new ArrayList<>(Arrays.asList("leet", "code"));
27+
assertEquals(true, modifiedDpAndPruningSolution.wordBreak(s, wordDict));
28+
}
29+
}

0 commit comments

Comments
 (0)