-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_131.java
More file actions
76 lines (69 loc) · 1.86 KB
/
Solution_131.java
File metadata and controls
76 lines (69 loc) · 1.86 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
131. 分割回文串
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。
返回 s 所有可能的分割方案。
示例:
输入: "aab"
输出:
[
["aa","b"],
["a","a","b"]
]
*/
import java.util.ArrayList;
import java.util.List;
public class Solution {
public static void main(String[] args) {
String s = "a";
Solution solution = new Solution();
System.out.println(solution.partition(s));
}
boolean[][] isPali;
String str;
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<>();
if(s == null || s.length() == 0){
return res;
}
isPali = calcuPali(s);
str = s;
dfs(res , new ArrayList<>() , 0);
return res;
}
public boolean[][] calcuPali(String s){
int n = s.length();
boolean[][] isPali = new boolean[n][n];
char[] ch = s.toCharArray();
int i, j;
for(int mid = 0; mid < n; mid ++){
i = mid;
j = mid;
while(i >= 0 && j < n && ch[i] == ch[j]){
isPali[i][j] = true;
i --;
j ++;
}
i = mid;
j = mid + 1;
while(i >= 0 && j < n && ch[i] == ch[j]){
isPali[i][j] = true;
i --;
j ++;
}
}
return isPali;
}
public void dfs(List<List<String>> res , List<String> list , int index){
if(index == str.length()){
res.add(new ArrayList<>(list));
return ;
}
for(int i = index + 1; i <= str.length(); i ++){
if(isPali[index][i - 1]){
list.add(str.substring(index , i));
dfs(res , list , i);
list.remove(list.size() - 1);
}
}
}
}