-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path131-splitPalin.java
More file actions
36 lines (33 loc) · 914 Bytes
/
131-splitPalin.java
File metadata and controls
36 lines (33 loc) · 914 Bytes
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
import java.util.*;
class Solution_131 {
List<String> cur = new ArrayList<>();
List<List<String>> res = new ArrayList<>();
public List<List<String>> partition(String s) {
if(s.length() == 0) return res;
helper(s, 0, s.length()-1);
return res;
}
void helper(String s, int i, int j) {
if(i > j) {
res.add(new ArrayList<String>(cur));
return;
}
for(int index=i; index<=j; index++) {
if(isPalindrome(s, i, index)) {
cur.add(s.substring(i, index+1));
helper(s, index+1, j);
cur.remove(cur.size()-1);
}
}
}
boolean isPalindrome(String s, int i, int j) {
while(i<j) {
if(s.charAt(i) != s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
}