-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode131.java
35 lines (33 loc) · 985 Bytes
/
leetcode131.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
public class Solution {
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<List<String>>();
if(s.length() == 0){
return res;
}
dfs(res,new ArrayList<String>(),s);
return res;
}
private void dfs(List<List<String>> res, List<String> path, String s){
if(s.length() == 0){
res.add(new ArrayList<String>(path));
return;
}
for(int i = 0; i < s.length(); i++){
if(isPalindrome(s, 0, i)){
path.add(s.substring(0,i+1));
dfs(res,path,s.substring(i+1));
path.remove(path.size()-1);
}
}
}
private boolean isPalindrome(String s, int start, int end){
while(start <= end){
if(s.charAt(start) != s.charAt(end)){
return false;
}
start++;
end--;
}
return true;
}
}