-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllPalindromicPartitions.java
More file actions
38 lines (32 loc) · 1 KB
/
AllPalindromicPartitions.java
File metadata and controls
38 lines (32 loc) · 1 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
package BackTracking;
import java.util.ArrayList;
public class AllPalindromicPartitions {
static ArrayList<ArrayList<String>> allPalindromicPerms(String s) {
ArrayList<ArrayList<String>> res = new ArrayList<>();
helper(res, new ArrayList<>(), 0, s);
return res;
}
private static void helper(ArrayList<ArrayList<String>> res, ArrayList<String> curr, int i, String s) {
if (i >= s.length()) {
res.add(new ArrayList<>(curr));
return;
}
for (int j = i; j < s.length(); j++) {
if (isPalindrome(s, i, j)) {
curr.add(s.substring(i, j + 1));
helper(res, curr, j + 1, s);
curr.remove(curr.size() - 1);
}
}
}
private static boolean isPalindrome(String s, int l, int r) {
while (l < r) {
if (s.charAt(l) != s.charAt(r)) {
return false;
}
l++;
r--;
}
return true;
}
}