forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
38 lines (38 loc) · 1.09 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
class Solution {
private List<List<String>> res;
public List<List<String>> partition(String s) {
res= new ArrayList<>();
func(new ArrayList<>(),0,s);
return res;
}
private void func(List<String> temp, int start, String str){
if(start>=str.length()){
res.add(new ArrayList<>(temp));
return;
}
int ed=str.indexOf(str.charAt(start),start+1);
while(ed>0){
int s=start;
int e=ed;
boolean flag=false;
while(s<e){
if(str.charAt(s)==str.charAt(e)){
s++;
e--;
} else{
flag=true;
break;
}
}
if(!flag){
temp.add(str.substring(start,ed+1));
func(temp,ed+1,str);
temp.remove(temp.size()-1);
}
ed=str.indexOf(str.charAt(start),ed+1);
}
temp.add(str.substring(start,start+1));
func(temp,start+1,str);
temp.remove(temp.size()-1);
}
}