-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
42 lines (42 loc) · 1.29 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
39
40
41
42
public class Solution {
Map<String,List<List<String>>> map=new HashMap<String, List<List<String>>>();
public List<List<String>> partition(String s) {
if (map.get(s)!=null) {
return map.get(s);
}
List<List<String>> ans=new ArrayList<List<String>>();
if(isPartition(s)){
List<String> list=new ArrayList<String>();
list.add(s);
ans.add(list);
}
for (int i = 0; i < s.length()-1; i++) {
String k=s.substring(0,i+1);
if (isPartition(k)) {
String t=s.substring(i+1,s.length());
List<List<String>> list=partition(t);
for (int j = 0; j < list.size(); j++) {
List<String> l=new ArrayList<String>();
l.add(k);
l.addAll(list.get(j));
ans.add(l);
}
}
}
map.put(s, ans);
return ans;
}
public boolean isPartition(String s){
if(s.length()==1){
return true;
}
char[] c=s.toCharArray();
int length=c.length;
for (int i = 0; i < length/2; i++) {
if(c[i]!=c[length-1-i]){
return false;
}
}
return true;
}
}