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 Result
What is the output of the following JavaScript code that finds all palindrome partitions of the string 'aab'?
DSA Javascript
function isPalindrome(s) {
let left = 0, right = s.length - 1;
while (left < right) {
if (s[left] !== s[right]) return false;
left++;
right--;
}
return true;
}
function backtrack(s, start, path, result) {
if (start === s.length) {
result.push([...path]);
return;
}
for (let end = start; end < s.length; end++) {
let substr = s.substring(start, end + 1);
if (isPalindrome(substr)) {
path.push(substr);
backtrack(s, end + 1, path, result);
path.pop();
}
}
}
function palindromePartition(s) {
const result = [];
backtrack(s, 0, [], result);
return result;
}
console.log(palindromePartition('aab'));Attempts:
2 left
💡 Hint
Think about all ways to split the string where each part is a palindrome.
✗ Incorrect
The code finds all partitions of 'aab' where each substring is a palindrome. 'a', 'a', 'b' are all palindromes, and 'aa' is also a palindrome, so the partitions are ["a","a","b"] and ["aa","b"].
🧠 Conceptual
intermediateUnderstanding Backtracking in Palindrome Partitioning
In palindrome partitioning using backtracking, what is the main reason for using backtracking?
Attempts:
2 left
💡 Hint
Backtracking helps when you need to try many options and undo some choices.
✗ Incorrect
Backtracking explores all possible partitions by choosing substrings and then undoing those choices to try others, ensuring all palindrome partitions are found.
🔧 Debug
advancedIdentify the Error in Palindrome Partitioning Code
What error will the following code produce when run, and why?
DSA Javascript
function isPalindrome(s) {
for (let i = 0; i < s.length / 2; i++) {
if (s[i] !== s[s.length - i - 1]) return false;
}
return true;
}
function backtrack(s, start, path, result) {
if (start === s.length) {
result.push([...path]);
return;
}
for (let end = start; end < s.length; end++) {
let substr = s.substring(start, end + 1);
if (isPalindrome(substr)) {
path.push(substr);
backtrack(s, end + 1, path, result);
path.pop();
}
}
}
function palindromePartition(s) {
const result = [];
backtrack(s, 0, [], result);
return result;
}
console.log(palindromePartition('aba'));Attempts:
2 left
💡 Hint
Check the palindrome check logic carefully, especially the index used.
✗ Incorrect
The code uses s[s.length - i] instead of s[s.length - i - 1], so it compares wrong characters and returns false negatives, causing incorrect partitions.
❓ Predict Output
advancedOutput of Palindrome Partitioning with Repeated Characters
What is the output of the following code for palindrome partitioning of 'aaa'?
DSA Javascript
function isPalindrome(s) {
let left = 0, right = s.length - 1;
while (left < right) {
if (s[left] !== s[right]) return false;
left++;
right--;
}
return true;
}
function backtrack(s, start, path, result) {
if (start === s.length) {
result.push([...path]);
return;
}
for (let end = start; end < s.length; end++) {
let substr = s.substring(start, end + 1);
if (isPalindrome(substr)) {
path.push(substr);
backtrack(s, end + 1, path, result);
path.pop();
}
}
}
function palindromePartition(s) {
const result = [];
backtrack(s, 0, [], result);
return result;
}
console.log(palindromePartition('aaa'));Attempts:
2 left
💡 Hint
Consider all ways to split 'aaa' into palindromes.
✗ Incorrect
All single characters 'a' are palindromes, 'aa' is palindrome, and 'aaa' is palindrome. So all partitions with these substrings are included.
🧠 Conceptual
expertTime Complexity of Palindrome Partitioning Using Backtracking
What is the time complexity of palindrome partitioning using backtracking for a string of length n, considering all possible partitions?
Attempts:
2 left
💡 Hint
Think about how many ways to partition and how palindrome checks add up.
✗ Incorrect
There are about 2^(n-1) ways to partition a string. Each palindrome check can take up to O(n), so total complexity is O(n * 2^n).
