-
Notifications
You must be signed in to change notification settings - Fork 13
/
Backtracking_PalindromePartitioning.java
88 lines (72 loc) · 3.43 KB
/
Backtracking_PalindromePartitioning.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
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
77
78
79
80
81
82
83
84
85
86
87
88
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
// https://www.youtube.com/watch?v=4ykBXGbonlA (best video explanation)
// https://leetcode.com/problems/palindrome-partitioning/
// https://leetcode.com/problems/palindrome-partitioning-ii/ (Similar Question)
// https://www.youtube.com/watch?v=A0wENqSIxK4
// https://leetcode.com/problems/palindrome-partitioning/discuss/41963/Java%3A-Backtracking-solution.
// https://www.youtube.com/results?search_query=Partition+string+into+palindromes
// https://www.geeksforgeeks.org/given-a-string-print-all-possible-palindromic-partition/
// https://www.geeksforgeeks.org/print-palindromic-partitions-string/
// https://github.com/mission-peace/interview/blob/master/src/com/interview/dynamic/PalindromePartition.java
// https://practice.geeksforgeeks.org/problems/palindromic-patitioning/0 (Similar Question)
// time complexity : O(n * (2^n))
// space complexity : O(n) (call stack)
/*
* input : nitin
* output : [ {n, i, t, i, n}, {n, iti, n}, {nitin} ]
* */
public class Backtracking_PalindromePartitioning {
private static List<List<String>> partition(String s) {
List<List<String>> decompositions = new ArrayList<>();
decomposeString(s, 0, new ArrayList<>(), decompositions);
return decompositions;
}
private static void decomposeString(String s, int startIndex, List<String> partialDecomposition,
List<List<String>> decompositions) {
if(startIndex == s.length()) { // If we have decomposed the whole string then return
decompositions.add(new ArrayList<>(partialDecomposition));
return;
}
for(int endIndex = startIndex; endIndex < s.length(); endIndex++) {
String subString = s.substring(startIndex, endIndex+1);
// Only recurse if the substring is a palindrome
if(isPalindrome(subString)) {
// 1.) Choose - Take the subString & add it to our decomposition 'path'
partialDecomposition.add(subString);
// 2.) Explore - Recurse and advance progress 1 past right bound of the 'palindromicSnippet' which is 'i + 1'
decomposeString(s, endIndex + 1, partialDecomposition, decompositions);
// 3.) Unchoose - We are done searching, remove the snippet from our 'path'. Next loop iteration will try another snippet in this stack frame.
partialDecomposition.remove(partialDecomposition.size() - 1);
}
}
}
private static boolean isPalindrome(String s) {
return s.equals(new StringBuffer(s).reverse().toString());
}
private boolean isPalindrome(int left, int right, String s) {
while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while(t-->0) {
String s = br.readLine().trim();
List<List<String>> output = partition(s);
for (List<String> list : output) {
System.out.print(list + " ");
}
System.out.println();
}
}
}