File tree 1 file changed +38
-0
lines changed
solution/0131.Palindrome Partitioning
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments