Skip to content

Commit 977ba51

Browse files
committed
Time: 6 ms (98.26%) | Memory: 55.3 MB (73.25%) - LeetSync
1 parent 9065491 commit 977ba51

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public List<List<String>> partition(String s) {
3+
List<List<String>> res = new ArrayList<>();
4+
List<String> path = new ArrayList<>();
5+
func(0,s,path,res);
6+
return res;
7+
}
8+
void func(int index, String s, List<String> path, List<List<String>> res){
9+
if(index==s.length()){
10+
res.add(new ArrayList<>(path));
11+
return;
12+
}
13+
for(int i=index;i<s.length();++i){
14+
if(isPalindrome(s,index,i)){
15+
path.add(s.substring(index,i+1));
16+
func(i+1,s,path,res);
17+
path.remove(path.size()-1);
18+
}
19+
}
20+
}
21+
boolean isPalindrome(String s,int start,int end){
22+
while(start<=end){
23+
if(s.charAt(start++)!=s.charAt(end--))
24+
return false;
25+
}
26+
return true;
27+
}
28+
}

0 commit comments

Comments
 (0)