We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 614d70f commit b2b872fCopy full SHA for b2b872f
MEDIUM/src/medium/WordBreak.java
@@ -0,0 +1,26 @@
1
+package medium;
2
+
3
+import java.util.Set;
4
5
+public class WordBreak {
6
7
+ //Jiuzhang gives a very good illustration for this problem!
8
+ public boolean wordBreak(String s, Set<String> wordDict) {
9
+ int n = s.length();
10
+ boolean[] dp = new boolean[n+1];
11
+ dp[0] = true;
12
+ for(int i = 1; i <= n; i++){
13
+ for(int j = 0; j < i; j++){
14
+ if(!dp[j]) continue;
15
16
+ String sub = s.substring(j, i);
17
+ if(wordDict.contains(sub)){
18
+ dp[i] = true;
19
+ break;
20
+ }
21
22
23
+ return dp[n];
24
25
26
+}
0 commit comments