Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the minimum cuts array with the maximum possible cuts.
DSA Javascript
const minCuts = new Array(n).fill([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Initializing minCuts with 0 which assumes no cuts needed initially.
Using Infinity which is not necessary here.
✗ Incorrect
We initialize minCuts with n-1 because the maximum cuts needed is one less than the length of the string.
2fill in blank
mediumComplete the code to check if the substring from j to i is a palindrome.
DSA Javascript
if (s[j] === s[i] && (i - j < 2 || [1][j + 1][i - 1])) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using minCuts instead of the palindrome table.
Using an undefined variable.
✗ Incorrect
The dp table named isPalindrome stores whether substrings are palindromes.
3fill in blank
hardFix the error in updating the minimum cuts when the substring is a palindrome.
DSA Javascript
minCuts[i] = j === 0 ? 0 : Math.min(minCuts[i], [1] + 1);
Attempts:
3 left
💡 Hint
Common Mistakes
Using minCuts[j] which is the cuts at the start index, not before it.
Using minCuts[i - 1] which relates to the current end index.
✗ Incorrect
We add 1 to the minimum cuts needed before index j, which is minCuts[j - 1].
4fill in blank
hardFill both blanks to correctly update the palindrome table and minimum cuts inside the nested loops.
DSA Javascript
isPalindrome[j][i] = true; minCuts[i] = j === 0 ? 0 : Math.min(minCuts[i], [1] + [2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using minCuts[i - 1] instead of minCuts[j - 1].
Adding 0 instead of 1 to the cuts.
✗ Incorrect
We set isPalindrome[j][i] to true and update minCuts[i] by adding 1 to minCuts[j - 1].
5fill in blank
hardFill all three blanks to complete the palindrome partitioning minimum cuts function.
DSA Javascript
function minCut(s) {
const n = s.length;
const minCuts = new Array(n).fill([1]);
const isPalindrome = Array.from({ length: n }, () => new Array(n).fill(false));
for (let i = 0; i < n; i++) {
for (let j = 0; j <= i; j++) {
if (s[j] === s[i] && (i - j < 2 || isPalindrome[j + 1][i - 1])) {
isPalindrome[j][i] = true;
minCuts[i] = j === 0 ? 0 : Math.min(minCuts[i], minCuts[[2]] + [3]);
}
}
}
return minCuts[n - 1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Initializing minCuts with 0.
Using wrong indices for minCuts update.
Adding 0 instead of 1 to cuts.
✗ Incorrect
Initialize minCuts with n-1, update minCuts[i] using minCuts[j-1] plus 1 cut.
