Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the DP array with maximum cuts possible for each position.
DSA Rust
let n = s.len(); let mut cuts = vec![[1]; n + 1];
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing cuts with 0 causes incorrect minimum cuts calculation.
Using usize::MAX causes overflow in later calculations.
✗ Incorrect
We initialize the cuts array with the maximum cuts possible, which is equal to the length of the string n. This represents the worst case where every character is cut separately.
2fill in blank
mediumComplete the code to mark the substring s[left..=right] as palindrome in the DP table.
DSA Rust
if s.as_bytes()[left] == s.as_bytes()[right] && (right - left < 2 || dp[left + 1][right - 1]) { dp[left][right] = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[left][right] to false prevents recognizing palindromes.
Using conditions like left == right instead of true.
✗ Incorrect
We set dp[left][right] to true to indicate that the substring s[left..=right] is a palindrome.
3fill in blank
hardFix the error in updating the minimum cuts when a palindrome substring is found.
DSA Rust
if dp[j][i] { cuts[i + 1] = cuts[i + 1].min(cuts[[1]] + 1); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using cuts[j + 1] causes index out of bounds or wrong calculation.
Using cuts[i] or cuts[i + 1] inside min causes no update.
✗ Incorrect
We use cuts[j] because cuts[j] represents the minimum cuts needed for substring s[0..j], so adding 1 cut after j gives cuts for s[0..i+1].
4fill in blank
hardFill both blanks to complete the nested loops iterating over substring indices.
DSA Rust
for [1] in 0..n { for [2] in 0..=i { // palindrome check and cuts update } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using same variable name for both loops causes errors.
Using n in inner loop causes out of range indices.
✗ Incorrect
The outer loop variable is i iterating over substring end indices, and inner loop variable is j iterating over substring start indices.
5fill in blank
hardFill all three blanks to complete the palindrome partitioning minimum cuts function.
DSA Rust
fn min_cut(s: &str) -> usize {
let n = s.len();
let mut dp = vec![vec![false; n]; n];
let mut cuts = vec![0; n + 1];
for i in 0..=n {
cuts[i] = [1];
}
for i in 0..n {
for j in 0..=i {
if s.as_bytes()[i] == s.as_bytes()[j] && (i - j < 2 || dp[j + 1][i - 1]) {
dp[j][i] = [2];
cuts[i + 1] = cuts[i + 1].min(cuts[[3]] + 1);
}
}
}
cuts[n] - 1
}Attempts:
3 left
💡 Hint
Common Mistakes
Initializing cuts with i causes wrong minimum cuts.
Setting dp[j][i] to false prevents palindrome recognition.
Using cuts[i] instead of cuts[j] in update causes errors.
✗ Incorrect
Initialize cuts[i] with n (max cuts), mark dp[j][i] as true for palindrome, and update cuts[i+1] using cuts[j] + 1.
