-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathSolution.java
38 lines (33 loc) · 1.14 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package g0101_0200.s0139_word_break;
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table
// #Dynamic_Programming #Trie #Memoization #Algorithm_II_Day_15_Dynamic_Programming
// #Dynamic_Programming_I_Day_9 #Udemy_Dynamic_Programming #Big_O_Time_O(M+max*N)_Space_O(M+N+max)
// #2024_11_15_Time_1_ms_(99.42%)_Space_42.1_MB_(80.42%)
import java.util.List;
public class Solution {
private Boolean[] memo;
public boolean wordBreak(String s, List<String> wordDict) {
memo = new Boolean[s.length() + 1];
return dp(s, 0, wordDict);
}
public boolean dp(String s, int i, List<String> wordDict) {
if (i == s.length()) {
return true;
}
if (memo[i] != null) {
return memo[i];
}
for (String word : wordDict) {
int len = word.length();
if (i + len > s.length() || !s.substring(i, i + len).equals(word)) {
continue;
}
if (dp(s, i + len, wordDict)) {
memo[i] = true;
return true;
}
}
memo[i] = false;
return false;
}
}