forked from Soumik-7031/SDESheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromePartitioningJava
More file actions
30 lines (28 loc) · 892 Bytes
/
PalindromePartitioningJava
File metadata and controls
30 lines (28 loc) · 892 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
class Solution {
public List<List<String>> partition(String s) {
List<List<String>> res= new ArrayList<>();
List<String> path = new ArrayList<>();
func(0, s, path, res);
return res;
}
void func(int index, String s, List<String> path, List<List<String>> res) {
if(index == s.length()) {
res.add(new ArrayList<>(path));
return;
}
for(int i = index; i < s.length(); ++i) {
if(isPalindrome(s, index, i)) {
path.add(s.substring(index, i+1));
func(i+1, s, path, res);
path.remove(path.size()-1);
}
}
}
boolean isPalindrome(String s, int start, int end) {
while(start <= end) {
if(s.charAt(start++) != s.charAt(end--))
return false;
}
return true;
}
}