0
0
DSA Typescriptprogramming~20 mins

Palindrome Partitioning DP Minimum Cuts in DSA Typescript - 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 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"));
A1
B0
C2
D3
Attempts:
2 left
💡 Hint
Think about how the string "aab" can be split into palindromes with minimum cuts.
🧠 Conceptual
intermediate
1: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'?
ATo store whether the substring s[start..end] is a palindrome
BTo count the number of palindrome substrings in s
CTo keep track of minimum cuts needed for each substring
DTo store the length of the longest palindrome substring
Attempts:
2 left
💡 Hint
Consider what information is needed to decide if a substring is palindrome quickly.
Predict Output
advanced
2: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"));
A0
B2
C3
D1
Attempts:
2 left
💡 Hint
Try to find palindrome partitions with minimum cuts for "abccbc".
🔧 Debug
advanced
2: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"));
ANo error, outputs 2
BTypeError: Cannot read property of undefined
CSyntaxError due to missing colon
DWrong output: 1 instead of 2
Attempts:
2 left
💡 Hint
Check the dp index used inside Math.min for updating minCuts.
🚀 Application
expert
3: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?
A3
B1
C2
D0
Attempts:
2 left
💡 Hint
Try to find palindrome substrings like "noon", "abba", and "d".