5
5
6
6
/**
7
7
* 139. Word Break
8
+ *
8
9
* Given a non-empty string s and a dictionary wordDict containing a list of non-empty words,
9
10
* determine if s can be segmented into a space-separated sequence of one or more dictionary words.
10
11
* You may assume the dictionary does not contain duplicate words.
@@ -22,10 +23,8 @@ The wordDict parameter had been changed to a list of strings (instead of a set o
22
23
23
24
public class _139 {
24
25
25
- public static class PureDPSolution {
26
- /**
27
- * This beats 70.10% submissions.
28
- */
26
+ public static class Solution1 {
27
+ /**this beats 70.46% submission. */
29
28
public boolean wordBreak (String s , List <String > wordDict ) {
30
29
int n = s .length ();
31
30
boolean [] dp = new boolean [n + 1 ];
@@ -42,9 +41,10 @@ public boolean wordBreak(String s, List<String> wordDict) {
42
41
}
43
42
}
44
43
45
- public static class ModifiedDPAndPruningSolution {
44
+ public static class Solution2 {
46
45
/**
47
- * This beats 86.09% submissions.
46
+ * Added pruning.
47
+ * this beats 89.91% submissions.
48
48
*/
49
49
public boolean wordBreak (String s , List <String > wordDict ) {
50
50
int maxLen = Integer .MIN_VALUE ;
@@ -70,9 +70,10 @@ public boolean wordBreak(String s, List<String> wordDict) {
70
70
}
71
71
}
72
72
73
- public static class DPAndPruningSolution {
73
+ public static class Solution3 {
74
74
/**
75
- * This beats 97.08% submissions.
75
+ * Added pruning, plus start from the end to check.
76
+ * This beats 95.20% submissions.
76
77
*/
77
78
public boolean wordBreak (String s , Set <String > wordDict ) {
78
79
int maxLen = Integer .MIN_VALUE ;
0 commit comments