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" using backtracking?
DSA C++
void backtrack(string s, int start, vector<string>& current, vector<vector<string>>& result) { if (start == s.size()) { result.push_back(current); return; } for (int end = start; end < s.size(); ++end) { bool palindrome = true; for (int i = start, j = end; i < j; ++i, --j) { if (s[i] != s[j]) { palindrome = false; break; } } if (palindrome) { current.push_back(s.substr(start, end - start + 1)); backtrack(s, end + 1, current, result); current.pop_back(); } } } vector<vector<string>> partition(string s) { vector<vector<string>> result; vector<string> current; backtrack(s, 0, current, result); return result; } int main() { string s = "aab"; vector<vector<string>> res = partition(s); for (auto &v : res) { for (auto &str : v) { cout << str << " "; } cout << "\n"; } return 0; }
Attempts:
2 left
💡 Hint
Check all substrings that are palindromes and explore partitions recursively.
✗ Incorrect
The code finds all palindrome partitions of "aab". The palindromes are "a", "a", "b" and "aa", "b". So the output lists these partitions.
🧠 Conceptual
intermediateNumber of Palindrome Partitions for "racecar"
How many palindrome partitions does the string "racecar" have when using backtracking to find all partitions?
Attempts:
2 left
💡 Hint
Consider all palindromic substrings and how they combine.
✗ Incorrect
The string "racecar" has 4 palindrome partitions including single letters and longer palindromes like "racecar" itself.
🔧 Debug
advancedIdentify the Error in Palindrome Partitioning Code
What error will the following code produce when trying to find palindrome partitions of "abc"?
void backtrack(string s, int start, vector& current, vector>& result) {
if (start > s.size()) {
result.push_back(current);
return;
}
for (int end = start; end < s.size(); ++end) {
bool palindrome = true;
for (int i = start, j = end; i < j; ++i, --j) {
if (s[i] != s[j]) {
palindrome = false;
break;
}
}
if (palindrome) {
current.push_back(s.substr(start, end - start + 1));
backtrack(s, end + 1, current, result);
current.pop_back();
}
}
}
Attempts:
2 left
💡 Hint
Check the base case condition carefully.
✗ Incorrect
The base case uses 'start > s.size()', but it should be 'start == s.size()' to stop recursion properly. Otherwise, no partitions are added because the base case never triggers.
🚀 Application
advancedFind the Palindrome Partitioning of "noon"
Which of the following lists contains all palindrome partitions of the string "noon"?
Attempts:
2 left
💡 Hint
Check all substrings that are palindromes: single letters, "oo", and "noon".
✗ Incorrect
The palindromic partitions are single letters, the middle "oo", and the whole string "noon".
❓ Predict Output
expertOutput of Palindrome Partitioning for "abba"
What is the output of the following C++ code that finds all palindrome partitions of the string "abba" using backtracking?
#include
#include
#include
using namespace std;
bool isPalindrome(const string& s, int start, int end) {
while (start < end) {
if (s[start++] != s[end--]) return false;
}
return true;
}
void backtrack(const string& s, int start, vector& current, vector>& result) {
if (start == s.size()) {
result.push_back(current);
return;
}
for (int end = start; end < s.size(); ++end) {
if (isPalindrome(s, start, end)) {
current.push_back(s.substr(start, end - start + 1));
backtrack(s, end + 1, current, result);
current.pop_back();
}
}
}
int main() {
string s = "abba";
vector> result;
vector current;
backtrack(s, 0, current, result);
for (auto& partition : result) {
for (auto& str : partition) {
cout << str << " ";
}
cout << "\n";
}
return 0;
}
Attempts:
2 left
💡 Hint
Check all palindromic substrings including single letters, "bb", and the whole string.
✗ Incorrect
The palindrome partitions of "abba" are the single letters, the middle "bb", and the entire string "abba".
