Skip to content

Commit 2e85a37

Browse files
committed
Word Break
1 parent e8ee61d commit 2e85a37

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

word-break/TonyKim9401.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// TC: O(n^2)
2+
// -> use 2 for-loops to search
3+
// SC: O(n)
4+
// -> boolean array's size
5+
class Solution {
6+
public boolean wordBreak(String s, List<String> wordDict) {
7+
Set<String> set = new HashSet(wordDict);
8+
9+
boolean[] dp = new boolean[s.length() + 1];
10+
dp[0] = true;
11+
12+
for (int i = 1; i <= s.length(); i++) {
13+
for (int j = 0; j < i; j++) {
14+
if (dp[j] && set.contains(s.substring(j, i))) {
15+
dp[i] = true;
16+
break;
17+
}
18+
}
19+
}
20+
return dp[s.length()];
21+
}
22+
}

0 commit comments

Comments
 (0)