From 16c61d436cdec305018c469757f8bf609cb7af66 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Tue, 4 Mar 2025 11:48:14 +0800 Subject: [PATCH] 20250304 finished. --- src/problem/mod.rs | 2 + .../p1745_palindrome_partitioning_iv.rs | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/problem/p1745_palindrome_partitioning_iv.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index aab7c22..82455a5 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -516,3 +516,5 @@ mod p131_palindrome_partitioning; mod p132_palindrome_partitioning_ii; mod p1278_palindrome_partitioning_iii; + +mod p1745_palindrome_partitioning_iv; diff --git a/src/problem/p1745_palindrome_partitioning_iv.rs b/src/problem/p1745_palindrome_partitioning_iv.rs new file mode 100644 index 0000000..c2abb55 --- /dev/null +++ b/src/problem/p1745_palindrome_partitioning_iv.rs @@ -0,0 +1,46 @@ +/** + * [1745] Palindrome Partitioning IV + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn check_partitioning(s: String) -> bool { + let s = s.as_bytes(); + let length = s.len(); + + let mut palindrome = vec![vec![true; length]; length]; + + for span in 2..=length { + for i in 0..=length - span { + let j = i + span - 1; + palindrome[i][j] = palindrome[i + 1][j - 1] && (s[i] == s[j]) + } + } + + for i in 0..length - 2 { + // 所有字符串都必须是非空的 + for j in i + 2..length { + if palindrome[0][i] && palindrome[i + 1][j - 1] && palindrome[j][length - 1] { + return true; + } + } + } + + false + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_1745() { + assert!(Solution::check_partitioning("abcbdd".to_owned())); + assert!(!Solution::check_partitioning("bcbddxy".to_owned())); + } +}