Palindrome Partitioning Using Backtracking in DSA C# - 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 we try all possible palindrome splits?
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 List<string>(current));
return;
}
for (int end = start; end < s.Length; end++) {
if (IsPalindrome(s, start, end)) {
current.Add(s.Substring(start, end - start + 1));
Backtrack(s, end + 1, current, result);
current.RemoveAt(current.Count - 1);
}
}
}
private bool IsPalindrome(string s, int left, int right) {
while (left < right) {
if (s[left++] != s[right--]) return false;
}
return true;
}
This code finds all ways to split a string into parts where each part is a palindrome, 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 calls explore all partitions.
- How many times: The recursion explores many combinations, and palindrome checks happen for each substring considered.
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 |
| 1000 | Extremely large, often impractical without optimization |
Pattern observation: The number of operations grows faster than any simple polynomial, roughly exponential with input size.
Time Complexity: O(n * 2^n)
This means the time grows very fast as the string gets longer, because we try many partitions and check palindromes for each.
[X] Wrong: "The algorithm runs in polynomial time because it just loops over the string."
[OK] Correct: The algorithm explores all possible partitions, which grow exponentially, not just a simple loop.
Understanding this complexity helps you explain why backtracking can be slow and when to expect exponential time, a key skill in interviews.
"What if we cached palindrome checks to avoid repeating them? 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 = all palindrome splits [OK]
- Confusing palindrome partitioning with finding longest palindrome
- Thinking palindrome partitioning only counts palindromes
- Assuming palindrome partitioning reverses the string
Solution
Step 1: Understand backtracking method requirements
The method should explore partitions and store results, so it returns void and uses parameters to track state.Step 2: Check method signatures
void Backtrack(string s, int start, List<string> currentPartition, List result) returns void and takes parameters for string, start index, current partition, and result list, matching backtracking needs.Final Answer:
void Backtrack(string s, int start, List<string> currentPartition, List result) -> Option DQuick Check:
Backtracking method returns void with state parameters [OK]
- Using return types other than void for backtracking
- Missing the result list parameter to store partitions
- Confusing return type with boolean or string
var result = new List();
Backtrack("aab", 0, new List<string>(), result);
Console.WriteLine(result.Count);
Assuming Backtrack partitions the string into all palindrome substrings correctly.Solution
Step 1: Identify palindrome partitions of "aab"
Possible palindrome partitions are ["a", "a", "b"] and ["aa", "b"].Step 2: Count all palindrome partitions
There are 2 partitions, so result.Count will be 2 after backtracking.Final Answer:
2 -> Option AQuick Check:
Palindrome partitions of "aab" = 2 [OK]
- Counting substrings instead of partitions
- Forgetting "aa" is a palindrome
- Assuming single partition only
void Backtrack(string s, int start, List<string> current, List result) {
if (start == s.Length) {
result.Add(new List<string>(current));
return;
}
for (int end = start; end < s.Length; end++) {
if (IsPalindrome(s, start, end)) {
current.Add(s.Substring(start, end - start));
Backtrack(s, end + 1, current, result);
current.RemoveAt(current.Count - 1);
}
}
}Solution
Step 1: Check substring length calculation
Substring(start, length) needs length = end - start + 1, but code uses end - start, missing last character.Step 2: Verify other parts
IsPalindrome parameters and RemoveAt usage are correct; base case is correct as start == s.Length.Final Answer:
Substring length calculation is incorrect -> Option BQuick Check:
Substring length = end - start + 1 [OK]
- Using substring length as end - start instead of end - start + 1
- Removing wrong index from current list
- Incorrect base case condition
Solution
Step 1: List all palindrome partitions of "racecar"
Partitions include ["r", "a", "c", "e", "c", "a", "r"], ["r", "a", "cec", "a", "r"], ["r", "aceca", "r"], ["racecar"], and others.Step 2: Count all valid palindrome partitions
Counting carefully, there are 10 valid palindrome partitions in total.Final Answer:
10 -> Option AQuick Check:
Palindrome partitions of "racecar" = 10 [OK]
- Counting only single character partitions
- Missing longer palindrome substrings like "cec" or "aceca"
- Assuming only 1 or 2 partitions
Ready to prove it?
Pass the test and this pattern moves unknown
