Challenge - 5 Problems
Palindrome Partitioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of minimum cuts for palindrome partitioning
What is the output of the following C++ code that calculates the minimum cuts needed to partition the string into palindromes?
DSA C++
int minCut(string s) { int n = s.size(); vector<vector<bool>> dp(n, vector<bool>(n, false)); vector<int> cuts(n, 0); for (int i = 0; i < n; i++) { int minCut = i; for (int j = 0; j <= i; j++) { if (s[j] == s[i] && (i - j < 2 || dp[j + 1][i - 1])) { dp[j][i] = true; minCut = j == 0 ? 0 : min(minCut, cuts[j - 1] + 1); } } cuts[i] = minCut; } return cuts[n - 1]; } int main() { string s = "aab"; cout << minCut(s) << endl; return 0; }
Attempts:
2 left
💡 Hint
Think about how many cuts are needed to split "aab" into palindromes.
✗ Incorrect
The string "aab" can be partitioned as "aa" | "b" with 1 cut. So the minimum cuts needed is 1.
🧠 Conceptual
intermediateUnderstanding the DP table in palindrome partitioning
In the palindrome partitioning DP solution, what does the 2D boolean table dp[i][j] represent?
Attempts:
2 left
💡 Hint
Think about what information helps decide if a substring is palindrome.
✗ Incorrect
dp[i][j] is true if the substring s[i..j] is a palindrome, false otherwise. This helps in deciding cuts.
🔧 Debug
advancedIdentify the error in the DP palindrome partitioning code
What error will the following code produce when run?
DSA C++
int minCut(string s) { int n = s.size(); vector<vector<bool>> dp(n, vector<bool>(n, false)); vector<int> cuts(n, 0); for (int i = 0; i < n; i++) { int minCut = i; for (int j = 0; j <= i; j++) { if (s[j] == s[i] && (i - j < 2 || dp[j + 1][i - 1])) { dp[j][i] = true; minCut = j == 0 ? 0 : min(minCut, cuts[j]); } } cuts[i] = minCut; } return cuts[n - 1]; } int main() { string s = "aab"; cout << minCut(s) << endl; return 0; }
Attempts:
2 left
💡 Hint
Check the index used to access cuts vector inside the inner loop.
✗ Incorrect
The code uses cuts[j] where j can be 0, but cuts[j-1] is needed to avoid out of range when j=0.
🚀 Application
advancedMinimum cuts for palindrome partitioning of a complex string
What is the minimum number of cuts needed to partition the string "ababbbabbababa" into palindromes?
Attempts:
2 left
💡 Hint
Try to find palindrome substrings and count cuts between them.
✗ Incorrect
The minimum cuts needed is 4 for this string based on palindrome partitions.
🧠 Conceptual
expertTime complexity of palindrome partitioning minimum cuts DP
What is the time complexity of the dynamic programming solution for palindrome partitioning minimum cuts on a string of length n?
Attempts:
2 left
💡 Hint
Consider the nested loops and palindrome checks.
✗ Incorrect
The DP solution uses nested loops and palindrome checks leading to O(n^3) time complexity.
