diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 288f268..4bfe421 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -512,3 +512,5 @@ mod p1472_design_browser_history; mod p2353_design_a_food_rating_system; mod p131_palindrome_partitioning; + +mod p132_palindrome_partitioning_ii; diff --git a/src/problem/p132_palindrome_partitioning_ii.rs b/src/problem/p132_palindrome_partitioning_ii.rs new file mode 100644 index 0000000..1e7ba50 --- /dev/null +++ b/src/problem/p132_palindrome_partitioning_ii.rs @@ -0,0 +1,51 @@ +/** + * [132] Palindrome Partitioning II + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn min_cut(s: String) -> i32 { + let s: Vec = s.chars().collect(); + let length = s.len(); + + let mut palindorme = vec![vec![true; length]; length]; + + for i in (0..length).rev() { + for j in i + 1..length { + palindorme[i][j] = (s[i] == s[j]) && palindorme[i + 1][j - 1] + } + } + + let mut dp = vec![i32::MAX; length]; + + for i in 0..length { + if palindorme[0][i] { + dp[i] = 0; + } else { + for j in 0..i { + if palindorme[j + 1][i] { + dp[i] = dp[i].min(dp[j] + 1); + } + } + } + } + + dp[length - 1] + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_132() { + assert_eq!(1, Solution::min_cut("aab".to_owned())); + assert_eq!(0, Solution::min_cut("a".to_owned())); + assert_eq!(1, Solution::min_cut("ab".to_owned())); + } +}