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 Java code that finds all palindrome partitions of the string "aab"?
DSA Java
import java.util.*; public class PalindromePartitioning { public static List<List<String>> partition(String s) { List<List<String>> result = new ArrayList<>(); backtrack(s, 0, new ArrayList<>(), result); return result; } private static 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 static boolean isPalindrome(String s, int left, int right) { while (left < right) { if (s.charAt(left++) != s.charAt(right--)) return false; } return true; } public static void main(String[] args) { List<List<String>> partitions = partition("aab"); System.out.println(partitions); } }
Attempts:
2 left
💡 Hint
Think about all ways to split the string into substrings that read the same forwards and backwards.
✗ Incorrect
The code finds all partitions where each substring is a palindrome. For "aab", the valid partitions are ["a", "a", "b"] and ["aa", "b"].
🧠 Conceptual
intermediateUnderstanding Backtracking in Palindrome Partitioning
In the palindrome partitioning backtracking approach, what is the main reason for removing the last added substring after the recursive call?
Attempts:
2 left
💡 Hint
Backtracking means trying and then undoing choices to try others.
✗ Incorrect
Removing the last added substring allows the algorithm to backtrack and try different partitions starting from the previous state.
🔧 Debug
advancedIdentify the Error in Palindrome Partitioning Code
What error will occur if the isPalindrome method is incorrectly implemented as below?
private static boolean isPalindrome(String s, int left, int right) {
while (left <= right) {
if (s.charAt(left) != s.charAt(right)) return false;
left++;
right--;
}
return true;
}
Attempts:
2 left
💡 Hint
Check if the loop condition and pointer updates are correct for palindrome checking.
✗ Incorrect
The loop condition 'left <= right' and pointer increments ensure all characters are checked correctly. This method works fine.
❓ Predict Output
advancedOutput of Palindrome Partitioning for "racecar"
What is the output of the palindrome partitioning code for the input string "racecar"?
DSA Java
import java.util.*; public class PalindromePartitioning { public static List<List<String>> partition(String s) { List<List<String>> result = new ArrayList<>(); backtrack(s, 0, new ArrayList<>(), result); return result; } private static 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 static boolean isPalindrome(String s, int left, int right) { while (left < right) { if (s.charAt(left++) != s.charAt(right--)) return false; } return true; } public static void main(String[] args) { List<List<String>> partitions = partition("racecar"); System.out.println(partitions); } }
Attempts:
2 left
💡 Hint
Look for all palindrome substrings and their combinations.
✗ Incorrect
The code finds all palindrome partitions including single letters and larger palindromes like "cec", "aceca", and the whole "racecar".
🚀 Application
expertMinimum Cuts for Palindrome Partitioning
Given the string "ababbbabbababa", what is the minimum number of cuts needed to partition it into palindromic substrings?
Attempts:
2 left
💡 Hint
Try to find the fewest cuts so that every substring is a palindrome.
✗ Incorrect
The minimum cuts needed for "ababbbabbababa" is 4, partitioning into palindromes like ["a", "babbbab", "b", "ababa"] or similar.
