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 code that calculates the minimum cuts needed to partition the string into palindromes?
DSA Python
def min_cut_palindrome(s): n = len(s) dp = [0] * n palindrome = [[False] * n for _ in range(n)] for i in range(n): min_cut = i for j in range(i + 1): if s[j] == s[i] and (i - j < 2 or palindrome[j + 1][i - 1]): palindrome[j][i] = True min_cut = 0 if j == 0 else min(min_cut, dp[j - 1] + 1) dp[i] = min_cut return dp[-1] print(min_cut_palindrome("aab"))
Attempts:
2 left
💡 Hint
Think about how many cuts are needed to split "aab" into palindromes.
✗ Incorrect
The string "aab" can be split into "aa" and "b" which are both palindromes. Only one cut is needed between the second and third character.
❓ Predict Output
intermediateMinimum cuts for a palindrome string
What is the output of the code below for the input string "racecar"?
DSA Python
def min_cut_palindrome(s): n = len(s) dp = [0] * n palindrome = [[False] * n for _ in range(n)] for i in range(n): min_cut = i for j in range(i + 1): if s[j] == s[i] and (i - j < 2 or palindrome[j + 1][i - 1]): palindrome[j][i] = True min_cut = 0 if j == 0 else min(min_cut, dp[j - 1] + 1) dp[i] = min_cut return dp[-1] print(min_cut_palindrome("racecar"))
Attempts:
2 left
💡 Hint
Is the whole string a palindrome?
✗ Incorrect
The entire string "racecar" is a palindrome, so no cuts are needed.
🧠 Conceptual
advancedUnderstanding the palindrome table in DP
In the palindrome partitioning DP solution, what does the 2D boolean table 'palindrome' represent?
Attempts:
2 left
💡 Hint
Think about how the DP checks if a substring is palindrome.
✗ Incorrect
The 'palindrome' table stores True if the substring s[j:i+1] is a palindrome, otherwise False.
❓ Predict Output
advancedMinimum cuts for complex string
What is the output of the following code for the input string "abccbc"?
DSA Python
def min_cut_palindrome(s): n = len(s) dp = [0] * n palindrome = [[False] * n for _ in range(n)] for i in range(n): min_cut = i for j in range(i + 1): if s[j] == s[i] and (i - j < 2 or palindrome[j + 1][i - 1]): palindrome[j][i] = True min_cut = 0 if j == 0 else min(min_cut, dp[j - 1] + 1) dp[i] = min_cut return dp[-1] print(min_cut_palindrome("abccbc"))
Attempts:
2 left
💡 Hint
Try to find palindrome partitions with minimum cuts.
✗ Incorrect
One optimal partition is "a" | "bccb" | "c" with 2 cuts.
🧠 Conceptual
expertTime complexity of palindrome partitioning 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 uses two nested loops over n and constant time palindrome checks using precomputed table, resulting in O(n^2).
