-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWord Break.java
More file actions
23 lines (23 loc) · 789 Bytes
/
Word Break.java
File metadata and controls
23 lines (23 loc) · 789 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static class Solution1 {
/**
* this solution takes between 7 and 8 ms to finish on LeetCode
* beats around 38% to 48% submissions as of 6/27/2020
*/
public boolean wordBreak(String s, List<String> wordDict) {
int n = s.length();
boolean[] dp = new boolean[n + 1];
dp[0] = true;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
if (dp[j]
&&
wordDict.contains(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
CommonUtils.printArray(dp);
return dp[n];
}
}