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 59284b5 commit d815b5cCopy full SHA for d815b5c
1 file changed
word-break/chjung99.java
@@ -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