|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
| 3 | +import java.util.List; |
3 | 4 | import java.util.Set;
|
4 | 5 |
|
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. |
6 | 11 |
|
7 | 12 | For example, given
|
8 | 13 | s = "leetcode",
|
|
11 | 16 | Return true because "leetcode" can be segmented as "leet code".
|
12 | 17 |
|
13 | 18 | 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 | + */ |
16 | 22 |
|
17 |
| - //Jiuzhang gives a very good illustration for this problem! |
| 23 | +public class _139 { |
18 | 24 |
|
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 | + } |
36 | 39 | }
|
37 | 40 | }
|
| 41 | + return dp[n]; |
38 | 42 | }
|
39 |
| - |
40 |
| - return dp[n]; |
41 | 43 | }
|
42 |
| - |
43 | 44 |
|
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 | + } |
59 | 65 | }
|
60 | 66 | }
|
| 67 | + return dp[n]; |
61 | 68 | }
|
62 |
| - |
63 |
| - return dp[n]; |
64 | 69 | }
|
65 | 70 |
|
| 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 | + } |
66 | 97 |
|
67 | 98 | }
|
0 commit comments