-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path131.分割回文串.java
50 lines (37 loc) · 1.02 KB
/
131.分割回文串.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* @lc app=leetcode.cn id=131 lang=java
*
* [131] 分割回文串
*/
// @lc code=start
class Solution {
List<List<String>> res=new ArrayList<>();
public List<List<String>> partition(String s) {
List<String> temp=new ArrayList<>();
helper(s,0,temp);
return res;
}
public void helper(String s,int start,List<String> temp){
if(start==s.length()){
res.add(new ArrayList<>(temp));
return ;
}
for(int i=start+1;i<=s.length();i++){
if(isValid(s.substring(start,i))){
temp.add(s.substring(start,i));
helper(s,i,temp);
temp.remove(temp.size()-1);
}
}
}
public boolean isValid(String str){
int n=str.length();
for(int i=0;i<n/2;i++){
if(str.charAt(i)!=str.charAt(n-i-1)){
return false;
}
}
return true;
}
}
// @lc code=end