@@ -5,30 +5,34 @@ public List<List<String>> partition(String s) {
5
5
}
6
6
7
7
List <List <String >> result = new ArrayList <>();
8
- dfs (0 , s , new ArrayList <>(), result );
8
+ helper (s , 0 , result , new ArrayList <>());
9
+
9
10
return result ;
10
11
}
11
12
12
- private void dfs ( int idx , String s , List <String > tempResult , List <List < String >> result ) {
13
+ private void helper ( String s , int idx , List <List < String >> result , List <String > temp ) {
13
14
if (idx == s .length ()) {
14
- result .add (new ArrayList <>(tempResult ));
15
+ result .add (new ArrayList <>(temp ));
15
16
return ;
16
17
}
17
18
18
19
for (int i = idx ; i < s .length (); i ++) {
19
20
if (isPalindrome (s , idx , i )) {
20
- tempResult .add (s .substring (idx , i + 1 ));
21
- dfs ( i + 1 , s , tempResult , result );
22
- tempResult .remove (tempResult .size () - 1 );
21
+ temp .add (s .substring (idx , i + 1 ));
22
+ helper ( s , i + 1 , result , temp );
23
+ temp .remove (temp .size () - 1 );
23
24
}
24
25
}
25
26
}
26
27
27
- private boolean isPalindrome (String s , int start , int end ) {
28
- while (start < end ) {
29
- if (s .charAt (start ++ ) != s .charAt (end -- )) {
28
+ private boolean isPalindrome (String s , int left , int right ) {
29
+ while (left < right ) {
30
+ if (s .charAt (left ) != s .charAt (right )) {
30
31
return false ;
31
32
}
33
+
34
+ ++left ;
35
+ --right ;
32
36
}
33
37
34
38
return true ;
0 commit comments