Skip to content

Commit b2b872f

Browse files
MEDIUM/src/medium/WordBreak.java
1 parent 614d70f commit b2b872f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

MEDIUM/src/medium/WordBreak.java

+26
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)