Palindrome Partitioning Using Backtracking in DSA Java - Time & Space Complexity
We want to understand how the time needed to find all palindrome partitions grows as the input string gets longer.
How does the number of steps change when the string size increases?
Analyze the time complexity of the following code snippet.
public void backtrack(String s, int start, List<String> current, List<List<String>> result) {
if (start == s.length()) {
result.add(new ArrayList<>(current));
return;
}
for (int end = start; end < s.length(); end++) {
if (isPalindrome(s, start, end)) {
current.add(s.substring(start, end + 1));
backtrack(s, end + 1, current, result);
current.remove(current.size() - 1);
}
}
}
private boolean isPalindrome(String s, int left, int right) {
while (left < right) {
if (s.charAt(left++) != s.charAt(right--)) return false;
}
return true;
}
This code finds all ways to split the string into palindrome parts using backtracking.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop tries every possible substring start to end, and the recursive call explores partitions from the next position.
- How many times: The recursion explores many combinations, and for each substring, palindrome checking scans characters between start and end.
As the string length grows, the number of palindrome partitions grows very fast because many substring combinations are possible.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Thousands of checks and recursive calls |
| 100 | Millions to billions of operations due to many partitions |
| 1000 | Extremely large, often impractical without optimization |
Pattern observation: The operations grow faster than any simple polynomial because of exploring all partition combinations and palindrome checks.
Time Complexity: O(n * 2^n)
This means the time grows very fast as the string gets longer, roughly doubling the work for each extra character, multiplied by the cost to check palindromes.
[X] Wrong: "The algorithm runs in simple polynomial time like O(n²) because it just checks substrings."
[OK] Correct: It explores all possible partitions, which grow exponentially, and palindrome checks add extra cost, making it much slower than polynomial time.
Understanding this complexity helps you explain why backtracking can be slow and when to consider optimizations or different approaches.
"What if we used memoization to store palindrome checks? How would the time complexity change?"
Practice
Solution
Step 1: Understand palindrome partitioning
Palindrome partitioning means dividing a string into parts where each part is a palindrome.Step 2: Role of backtracking
Backtracking helps explore all possible ways to split the string into palindrome parts.Final Answer:
To split a string into all possible palindrome substrings -> Option CQuick Check:
Palindrome partitioning = splitting into palindrome substrings [OK]
- Confusing palindrome partitioning with longest palindrome substring
- Thinking it only counts palindromes instead of listing partitions
- Assuming it reverses the whole string
Solution
Step 1: Analyze method parameters
The method needs the string, current start index, a list to store all partitions, and the current partition path.Step 2: Check return type
Backtracking methods usually return void because they build results via parameters.Final Answer:
void backtrack(String s, int start, List result, List<String> current) -> Option BQuick Check:
Backtracking method returns void and uses lists [OK]
- Using wrong return type like int or boolean
- Missing current partition list parameter
- Returning String instead of void
"aab" using palindrome partitioning?Solution
Step 1: Identify palindrome substrings
"a" and "aa" are palindromes; "b" is a palindrome; "ab" is not.Step 2: List all palindrome partitions
Partitions are ["a", "a", "b"] and ["aa", "b"].Final Answer:
[["a", "a", "b"], ["aa", "b"]] -> Option DQuick Check:
Only palindrome splits included [OK]
- Including non-palindrome substrings like "ab"
- Missing the partition with "aa"
- Returning only one partition
void backtrack(String s, int start, List result, List<String> current) {
if (start == s.length()) {
result.add(current);
return;
}
for (int end = start; end < s.length(); end++) {
if (isPalindrome(s, start, end)) {
current.add(s.substring(start, end + 1));
backtrack(s, end + 1, result, current);
current.remove(current.size());
}
}
}Solution
Step 1: Check removal from current list
List indices start at 0, so removing at current.size() causes IndexOutOfBoundsException.Step 2: Correct removal index
Should remove last element with current.remove(current.size() - 1).Final Answer:
Incorrect removal index in current.remove() -> Option AQuick Check:
Remove last element with size()-1 [OK]
- Removing element at size() instead of size()-1
- Confusing substring indices
- Incorrect recursive call start index
"racecar", how many palindrome partitions will the backtracking algorithm generate?Solution
Step 1: Identify palindrome substrings in "racecar"
Palindromes include "r", "a", "c", "e", "cec", "aceca", "racecar" and their combinations.Step 2: Count all palindrome partitions
Partitions are:
["racecar"],
["r", "aceca", "r"],
["r", "a", "cec", "a", "r"],
["r", "a", "c", "e", "c", "a", "r"],
and others including splits like ["r", "a", "c", "ec", "a", "r"] are invalid because "ec" is not palindrome.
Counting all valid partitions, the total is 6.Final Answer:
6 -> Option AQuick Check:
Count all palindrome partitions = 6 [OK]
- Counting only single-character partitions
- Missing longer palindrome substrings
- Double counting partitions
Ready to prove it?
Pass the test and this pattern moves unknown
