Challenge - 5 Problems
Palindrome Partitioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of minimum cuts for palindrome partitioning
What is the output of the following TypeScript code that calculates the minimum cuts needed to partition the string into palindromes?
DSA Typescript
function minCut(s: string): number {
const n = s.length;
const dp = new Array(n).fill(0);
const palindrome = Array.from({ length: n }, () => new Array(n).fill(false));
for (let end = 0; end < n; end++) {
let minCuts = end;
for (let start = 0; start <= end; start++) {
if (s[start] === s[end] && (end - start < 2 || palindrome[start + 1][end - 1])) {
palindrome[start][end] = true;
minCuts = start === 0 ? 0 : Math.min(minCuts, dp[start - 1] + 1);
}
}
dp[end] = minCuts;
}
return dp[n - 1];
}
console.log(minCut("aab"));Attempts:
2 left
💡 Hint
Think about how the string "aab" can be split into palindromes with minimum cuts.
✗ Incorrect
The string "aab" can be split into "aa" and "b", both palindromes, so only 1 cut is needed.
🧠 Conceptual
intermediate1:30remaining
Understanding the role of the palindrome table
In the palindrome partitioning DP solution, what is the main purpose of the 2D boolean array 'palindrome'?
Attempts:
2 left
💡 Hint
Consider what information is needed to decide if a substring is palindrome quickly.
✗ Incorrect
The 'palindrome' table stores true or false for each substring indicating if it is a palindrome, which helps avoid recomputing palindrome checks.
❓ Predict Output
advanced2:30remaining
Minimum cuts for a complex palindrome partitioning
What is the output of the following TypeScript code for the input string "abccbc"?
DSA Typescript
function minCut(s: string): number {
const n = s.length;
const dp = new Array(n).fill(0);
const palindrome = Array.from({ length: n }, () => new Array(n).fill(false));
for (let end = 0; end < n; end++) {
let minCuts = end;
for (let start = 0; start <= end; start++) {
if (s[start] === s[end] && (end - start < 2 || palindrome[start + 1][end - 1])) {
palindrome[start][end] = true;
minCuts = start === 0 ? 0 : Math.min(minCuts, dp[start - 1] + 1);
}
}
dp[end] = minCuts;
}
return dp[n - 1];
}
console.log(minCut("abccbc"));Attempts:
2 left
💡 Hint
Try to find palindrome partitions with minimum cuts for "abccbc".
✗ Incorrect
The minimum cuts needed are 2, for example: "a" | "bccb" | "c".
🔧 Debug
advanced2:00remaining
Identify the error in palindrome partitioning code
What error will the following TypeScript code produce when run?
DSA Typescript
function minCut(s: string): number {
const n = s.length;
const dp = new Array(n).fill(0);
const palindrome = Array.from({ length: n }, () => new Array(n).fill(false));
for (let end = 0; end < n; end++) {
let minCuts = end;
for (let start = 0; start <= end; start++) {
if (s[start] === s[end] && (end - start < 2 || palindrome[start + 1][end - 1])) {
palindrome[start][end] = true;
minCuts = start === 0 ? 0 : Math.min(minCuts, dp[start] + 1);
}
}
dp[end] = minCuts;
}
return dp[n - 1];
}
console.log(minCut("abc"));Attempts:
2 left
💡 Hint
Check the dp index used inside Math.min for updating minCuts.
✗ Incorrect
The code uses dp[start] instead of dp[start - 1], causing incorrect minimum cuts calculation.
🚀 Application
expert3:00remaining
Minimum cuts for palindrome partitioning of a long string
Given the string "noonabbad", what is the minimum number of cuts needed to partition it into palindromes?
Attempts:
2 left
💡 Hint
Try to find palindrome substrings like "noon", "abba", and "d".
✗ Incorrect
The string can be partitioned as "noon" | "abba" | "d", requiring 2 cuts.