Skip to content

Commit d815b5c

Browse files
committed
word-break
1 parent 59284b5 commit d815b5c

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

word-break/chjung99.java

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

0 commit comments

Comments
 (0)