-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
28 lines (28 loc) · 920 Bytes
/
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
public class Solution {
public boolean wordBreak(String s, Set<String> wordDict) {
return f(s,wordDict,new HashMap<String, Boolean>());
}
public boolean f(String s, Set<String> wordDict,Map<String, Boolean> map){
if (s==null||s.length()==0) {
return true;
}
if (map.get(s)!=null) {
return map.get(s);
}else {
for (int i = 0; i < s.length(); i++) {
String str=s.substring(0,i+1);
boolean flag=false;
for (String test : wordDict) {
if (str.equals(test)) {
flag=true;
if (f(s.substring(i+1,s.length()),wordDict,map)) {
return true;
}
}
}
map.put(str, flag);
}
return false;
}
}
}