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 Rust code that calculates the minimum cuts needed to partition the string into palindromes?
DSA Rust
fn min_cut(s: &str) -> i32 { let n = s.len(); let chars: Vec<char> = s.chars().collect(); let mut dp = vec![0; n + 1]; let mut palindrome = vec![vec![false; n]; n]; for i in 0..=n { dp[i] = i as i32 - 1; } for i in 0..n { for j in 0..=i { if chars[i] == chars[j] && (i - j < 2 || palindrome[j + 1][i - 1]) { palindrome[j][i] = true; dp[i + 1] = dp[i + 1].min(dp[j] + 1); } } } dp[n] } fn main() { let s = "aab"; println!("{}", min_cut(s)); }
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", both palindromes, with 1 cut.
The DP array dp keeps track of minimum cuts needed up to each position.
🧠 Conceptual
intermediateUnderstanding the DP state in palindrome partitioning
In the palindrome partitioning minimum cuts problem, what does the dp[i] array represent?
Attempts:
2 left
💡 Hint
Think about how dp is used to build the solution from the start of the string.
✗ Incorrect
dp[i] stores the minimum cuts needed to partition the substring from the start up to index i-1 into palindromes.
🔧 Debug
advancedIdentify the error in palindrome partitioning DP code
What error will this Rust code produce when run?
DSA Rust
fn min_cut(s: &str) -> i32 { let n = s.len(); let chars: Vec<char> = s.chars().collect(); let mut dp = vec![0; n]; let mut palindrome = vec![vec![false; n]; n]; for i in 0..=n { dp[i] = i as i32 - 1; } for i in 0..n { for j in 0..=i { if chars[i] == chars[j] && (i - j < 2 || palindrome[j + 1][i - 1]) { palindrome[j][i] = true; dp[i + 1] = dp[i + 1].min(dp[j] + 1); } } } dp[n] }
Attempts:
2 left
💡 Hint
Check the size of dp vector and the indices used to access it.
✗ Incorrect
The dp vector is initialized with size n, but the code accesses dp[i] for i in 0..=n, which causes an index out of bounds panic.
🚀 Application
advancedMinimum cuts for palindrome partitioning of a complex string
What is the minimum number of cuts needed to partition the string "abccbc" into palindromes?
Attempts:
2 left
💡 Hint
Try to find palindrome substrings that cover the string with few cuts.
✗ Incorrect
One optimal partition is "a" | "bccb" | "c" with 2 cuts.
❓ Predict Output
expertOutput of DP array after processing string "racecar"
Given the following Rust code snippet for palindrome partitioning minimum cuts, what is the final dp array printed after processing the string "racecar"?
DSA Rust
fn min_cut_dp_array(s: &str) -> Vec<i32> { let n = s.len(); let chars: Vec<char> = s.chars().collect(); let mut dp = vec![0; n + 1]; let mut palindrome = vec![vec![false; n]; n]; for i in 0..=n { dp[i] = i as i32 - 1; } for i in 0..n { for j in 0..=i { if chars[i] == chars[j] && (i - j < 2 || palindrome[j + 1][i - 1]) { palindrome[j][i] = true; dp[i + 1] = dp[i + 1].min(dp[j] + 1); } } } dp } fn main() { let s = "racecar"; let dp_array = min_cut_dp_array(s); println!("{:?}", dp_array); }
Attempts:
2 left
💡 Hint
The string "racecar" is a palindrome, so minimum cuts at the end should be 0.
✗ Incorrect
The dp array starts with dp[i] = i-1.
As palindrome substrings are found, dp values update to minimum cuts.
For "racecar", the entire string is palindrome, so dp[7] = 0.
