0
0
DSA Cprogramming~20 mins

Palindrome Partitioning DP Minimum Cuts in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Palindrome Partitioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of minimum cuts for palindrome partitioning
What is the output of the following code that calculates the minimum cuts needed to partition the string into palindromes?
DSA C
int minCut(char* s) {
    int n = strlen(s);
    int dp[n+1];
    int pal[n][n];
    for (int i = 0; i < n; i++) {
        dp[i] = n - 1 - i;
        for (int j = 0; j <= i; j++) {
            if (s[i] == s[j] && (i - j < 2 || pal[j+1][i-1])) {
                pal[j][i] = 1;
                if (j == 0) dp[i] = 0;
                else if (dp[j-1] + 1 < dp[i]) dp[i] = dp[j-1] + 1;
            } else {
                pal[j][i] = 0;
            }
        }
    }
    return dp[n-1];
}

// Input string: "aab" 
// Call: minCut("aab")
A1
B0
C2
D3
Attempts:
2 left
💡 Hint
Think about how many cuts are needed to split "aab" into palindromes.
🧠 Conceptual
intermediate
1:30remaining
Understanding the DP array in palindrome partitioning
In the palindrome partitioning minimum cuts problem, what does the dp array represent?
Adp[i] stores the maximum palindrome length ending at i
Bdp[i] stores the minimum cuts needed for substring s[i..end]
Cdp[i] stores the count of palindromes in s[0..i]
Ddp[i] stores the minimum cuts needed for substring s[0..i]
Attempts:
2 left
💡 Hint
Think about what the problem asks: minimum cuts for prefixes of the string.
🔧 Debug
advanced
2:00remaining
Identify the error in palindrome partitioning code
What error will the following code produce when run?
DSA C
int minCut(char* s) {
    int n = strlen(s);
    int dp[n];
    int pal[n][n];
    for (int i = 0; i < n; i++) {
        dp[i] = n - i;
        for (int j = 0; j <= i; j++) {
            if (s[i] == s[j] && (i - j < 2 || pal[j+1][i-1])) {
                pal[j][i] = 1;
                if (j == 0) dp[i] = 0;
                else if (dp[j-1] + 1 < dp[i]) dp[i] = dp[j-1] + 1;
            } else {
                pal[j][i] = 0;
            }
        }
    }
    return dp[n-1];
}

// Input: "aab"
ACompilation error due to uninitialized pal array
BArray index out of bounds error
CReturns incorrect minimum cuts
DNo error, returns correct result
Attempts:
2 left
💡 Hint
Check the initialization and indexing of dp array.
Predict Output
advanced
2:30remaining
Output of minimum cuts for palindrome partitioning with repeated characters
What is the output of the minimum cuts function for the input string "ababbbabbababa"?
DSA C
int minCut(char* s) {
    int n = strlen(s);
    int dp[n];
    int pal[n][n];
    for (int i = 0; i < n; i++) {
        dp[i] = i;
        for (int j = 0; j <= i; j++) {
            if (s[i] == s[j] && (i - j < 2 || pal[j+1][i-1])) {
                pal[j][i] = 1;
                if (j == 0) dp[i] = 0;
                else if (dp[j-1] + 1 < dp[i]) dp[i] = dp[j-1] + 1;
            } else {
                pal[j][i] = 0;
            }
        }
    }
    return dp[n-1];
}

// Call: minCut("ababbbabbababa")
A3
B4
C2
D5
Attempts:
2 left
💡 Hint
Try to find the minimum cuts to split the string into palindromes.
🧠 Conceptual
expert
1:30remaining
Time complexity of palindrome partitioning minimum cuts algorithm
What is the time complexity of the dynamic programming solution for palindrome partitioning minimum cuts problem?
AO(n log n)
BO(n^2)
CO(n^3)
DO(n)
Attempts:
2 left
💡 Hint
Consider the nested loops and palindrome checks.