Challenge - 5 Problems
Palindrome Partitioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Palindrome Partitioning for "aab"
What is the output of the following C# code that finds all palindrome partitions of the string "aab"?
DSA C#
using System; using System.Collections.Generic; class Solution { public IList<IList<string>> Partition(string s) { var result = new List<IList<string>>(); Backtrack(s, 0, new List<string>(), result); return result; } private void Backtrack(string s, int start, List<string> current, IList<IList<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; left++; right--; } return true; } } class Program { static void Main() { var sol = new Solution(); var partitions = sol.Partition("aab"); foreach (var partition in partitions) { Console.WriteLine(string.Join(",", partition)); } } }
Attempts:
2 left
💡 Hint
Check all substrings and verify if they are palindromes before adding to the partition.
✗ Incorrect
The code finds all partitions where each substring is a palindrome. For "aab", the partitions are ["a","a","b"] and ["aa","b"].
🧠 Conceptual
intermediateNumber of Palindrome Partitions for "racecar"
How many palindrome partitions does the string "racecar" have using backtracking?
Attempts:
2 left
💡 Hint
Consider all possible palindrome substrings and count partitions.
✗ Incorrect
The string "racecar" has 4 palindrome partitions when all possible palindrome substrings are considered.
🔧 Debug
advancedIdentify the Error in Palindrome Partitioning Code
What error will the following code produce when run for palindrome partitioning?
DSA C#
public void Backtrack(string s, int start, List<string> current, IList<IList<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; left++; right--; } return true; }
Attempts:
2 left
💡 Hint
Check the base case condition for the recursion.
✗ Incorrect
The base case uses 'start > s.Length' which allows start == s.Length + 1 causing substring calls to go out of range.
🚀 Application
advancedFind the Longest Palindromic Partition
Given the string "banana", which palindrome partition has the longest single palindrome substring?
Attempts:
2 left
💡 Hint
Look for the longest palindrome substring inside the partitions.
✗ Incorrect
"anana" is the longest palindrome substring in "banana", so the partition ["b", "anana"] contains the longest palindrome.
❓ Predict Output
expertOutput of Palindrome Partitioning for "abc"
What is the output of the following C# palindrome partitioning code for the input string "abc"?
DSA C#
using System; using System.Collections.Generic; class Solution { public IList<IList<string>> Partition(string s) { var result = new List<IList<string>>(); Backtrack(s, 0, new List<string>(), result); return result; } private void Backtrack(string s, int start, List<string> current, IList<IList<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; left++; right--; } return true; } } class Program { static void Main() { var sol = new Solution(); var partitions = sol.Partition("abc"); foreach (var partition in partitions) { Console.WriteLine(string.Join(",", partition)); } } }
Attempts:
2 left
💡 Hint
Check which substrings are palindromes in "abc".
✗ Incorrect
Only single characters are palindromes in "abc", so the only partition is ["a","b","c"].
