-
Notifications
You must be signed in to change notification settings - Fork 0
/
Word Break
26 lines (21 loc) · 912 Bytes
/
Word Break
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
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
Map<String, Boolean> map=new HashMap<>();
return processString(s,wordDict,map);
}
boolean processString(String s, List<String> wordDict, Map<String, Boolean> cache){
if(s.length()==0) return true;
if(wordDict.contains(s)) return true;
if(cache.containsKey(s)) return cache.get(s);
for(int i=1; i<=s.length(); i++){
String pre=s.substring(0,i);
if(wordDict.contains(pre) && processString(s.substring(i), wordDict, cache)){
cache.put(pre, true);
return true;
}
cache.put(pre,false);
}
return false;
}
}
#Status: Couldn't solve for one case, was getting TLE, then took help. Still not comfortable with DP approach, although I could understand the solution.