forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_139.java
90 lines (81 loc) · 2.89 KB
/
_139.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.fishercoder.solutions;
import com.fishercoder.common.utils.CommonUtils;
import java.util.List;
public class _139 {
public static class Solution1 {
/**
* this solution takes between 7 and 8 ms to finish on LeetCode
* beats around 38% to 48% submissions as of 6/27/2020
*/
public boolean wordBreak(String s, List<String> wordDict) {
int n = s.length();
boolean[] dp = new boolean[n + 1];
dp[0] = true;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
if (dp[j]
&&
wordDict.contains(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
CommonUtils.printArray(dp);
return dp[n];
}
}
public static class Solution2 {
/**
* Added pruning based on max word length.
* this solution takes between 2 and 3 ms to finish on LeetCode
* this beats 94.53% submissions as of 6/27/2020
*/
public boolean wordBreak(String s, List<String> wordDict) {
int maxLen = Integer.MIN_VALUE;
for (String word : wordDict) {
maxLen = (word.length() > maxLen) ? word.length() : maxLen;
}
int n = s.length();
boolean[] dp = new boolean[n + 1];
dp[0] = true;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
if ((i - j) > maxLen) {
continue;
}
if (dp[j] && wordDict.contains(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[n];
}
}
public static class Solution3 {
/**
* Added pruning, plus start from the end to check.
* This solution takes 1 ms to finish on LeetCode
* This beats 99.02% submissions as of 6/27/2020.
*/
public boolean wordBreak(String s, List<String> wordDict) {
int maxLen = Integer.MIN_VALUE;
for (String word : wordDict) {
maxLen = (word.length() > maxLen) ? word.length() : maxLen;
}
int n = s.length();
boolean[] dp = new boolean[n + 1];
dp[0] = true;
for (int i = 1; i <= n; i++) {
for (int lastWordLength = 1; lastWordLength <= i && lastWordLength <= maxLen; lastWordLength++) {
if (dp[i - lastWordLength] && wordDict.contains(s.substring(i - lastWordLength, i))) {
dp[i] = true;
break;
}
}
}
return dp[n];
}
}
}