Skip to content

Commit 584a087

Browse files
committed
Runtime: 20 ms (Top 16.65%) | Memory: 47.4 MB (Top 24.65%)
1 parent 6f46125 commit 584a087

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Runtime: 20 ms (Top 16.65%) | Memory: 47.4 MB (Top 24.65%)
2+
class Solution {
3+
Map<String,Boolean>map= new HashMap<>();
4+
public boolean wordBreak(String s, List<String> wordDict) {
5+
6+
if(wordDict.contains(s)){
7+
return true;
8+
}
9+
if(map.containsKey(s)){
10+
return map.get(s);
11+
}
12+
for(int i=0;i<s.length();++i){
13+
String left=s.substring(0,i);
14+
if(wordDict.contains(left) && wordBreak(s.substring(i),wordDict)){
15+
map.put(s,true);
16+
return true;
17+
}
18+
}
19+
map.put(s,false);
20+
return false;
21+
}
22+
}

0 commit comments

Comments
 (0)