Skip to content

Commit 51813d3

Browse files
committed
solve word break
1 parent 133cd80 commit 51813d3

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

word-break/sora0319.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class Solution {
2+
public boolean wordBreak(String s, List<String> wordDict) {
3+
boolean[] dp = new boolean[s.length() + 1];
4+
dp[0] = true;
5+
6+
for (int n = 1; n <= s.length(); n++) {
7+
for (String word : wordDict) {
8+
if (n >= word.length() && s.substring(n - word.length(), n).equals(word)) {
9+
dp[n] = dp[n - word.length()];
10+
}
11+
if (dp[n]) {
12+
break;
13+
}
14+
}
15+
}
16+
17+
return dp[s.length()];
18+
}
19+
}

0 commit comments

Comments
 (0)