Skip to content

Commit 4cb69ba

Browse files
131. Palindrome Partitioning (java)
1 parent 2e19617 commit 4cb69ba

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
private List<List<String>> res;
3+
public List<List<String>> partition(String s) {
4+
res= new ArrayList<>();
5+
func(new ArrayList<>(),0,s);
6+
return res;
7+
}
8+
private void func(List<String> temp, int start, String str){
9+
if(start>=str.length()){
10+
res.add(new ArrayList<>(temp));
11+
return;
12+
}
13+
int ed=str.indexOf(str.charAt(start),start+1);
14+
while(ed>0){
15+
int s=start;
16+
int e=ed;
17+
boolean flag=false;
18+
while(s<e){
19+
if(str.charAt(s)==str.charAt(e)){
20+
s++;
21+
e--;
22+
} else{
23+
flag=true;
24+
break;
25+
}
26+
}
27+
if(!flag){
28+
temp.add(str.substring(start,ed+1));
29+
func(temp,ed+1,str);
30+
temp.remove(temp.size()-1);
31+
}
32+
ed=str.indexOf(str.charAt(start),ed+1);
33+
}
34+
temp.add(str.substring(start,start+1));
35+
func(temp,start+1,str);
36+
temp.remove(temp.size()-1);
37+
}
38+
}

0 commit comments

Comments
 (0)