From f26f92eead3bb74df4d184cfe6f1184a29251dd1 Mon Sep 17 00:00:00 2001 From: EFanZh Date: Sat, 16 Sep 2023 12:41:03 +0800 Subject: [PATCH] Add problem 1616: Split Two Strings to Make Palindrome --- src/lib.rs | 1 + .../greedy.rs | 59 +++++++++++++++++++ .../mod.rs | 38 ++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 src/problem_1616_split_two_strings_to_make_palindrome/greedy.rs create mode 100644 src/problem_1616_split_two_strings_to_make_palindrome/mod.rs diff --git a/src/lib.rs b/src/lib.rs index b9a15ef0..f8d6ee87 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1329,6 +1329,7 @@ pub mod problem_1610_maximum_number_of_visible_points; pub mod problem_1611_minimum_one_bit_operations_to_make_integers_zero; pub mod problem_1614_maximum_nesting_depth_of_the_parentheses; pub mod problem_1615_maximal_network_rank; +pub mod problem_1616_split_two_strings_to_make_palindrome; pub mod problem_1619_mean_of_array_after_removing_some_elements; pub mod problem_1624_largest_substring_between_two_equal_characters; pub mod problem_1629_slowest_key; diff --git a/src/problem_1616_split_two_strings_to_make_palindrome/greedy.rs b/src/problem_1616_split_two_strings_to_make_palindrome/greedy.rs new file mode 100644 index 00000000..a43df4d8 --- /dev/null +++ b/src/problem_1616_split_two_strings_to_make_palindrome/greedy.rs @@ -0,0 +1,59 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + fn is_palindrome(mut iter: impl DoubleEndedIterator) -> bool { + while let Some(first) = iter.next() { + if let Some(last) = iter.next_back() { + if first != last { + return false; + } + } else { + break; + } + } + + true + } + + fn check(a: &[u8], b: &[u8]) -> bool { + let mut iter = a.iter().copied().zip(b.iter().copied()); + + while let Some((left_top, left_bottom)) = iter.next() { + if let Some((right_top, right_bottom)) = iter.next_back() { + if left_top != right_bottom { + return (left_top == right_top && Self::is_palindrome(iter.clone().map(|(top, _)| top))) + || (left_bottom == right_bottom && Self::is_palindrome(iter.map(|(_, bottom)| bottom))); + } + } else { + break; + } + } + + true + } + + pub fn check_palindrome_formation(a: String, b: String) -> bool { + let a = a.as_bytes(); + let b = b.as_bytes(); + + Self::check(a, b) || Self::check(b, a) + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn check_palindrome_formation(a: String, b: String) -> bool { + Self::check_palindrome_formation(a, b) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_1616_split_two_strings_to_make_palindrome/mod.rs b/src/problem_1616_split_two_strings_to_make_palindrome/mod.rs new file mode 100644 index 00000000..3a9cf40a --- /dev/null +++ b/src/problem_1616_split_two_strings_to_make_palindrome/mod.rs @@ -0,0 +1,38 @@ +pub mod greedy; + +pub trait Solution { + fn check_palindrome_formation(a: String, b: String) -> bool; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + (("x", "y"), true), + (("xbdef", "xecab"), false), + (("ulacfd", "jizalu"), true), + (("abda", "acmc"), false), + ( + ( + "aejbaalflrmkswrydwdkdwdyrwskmrlfqizjezd", + "uvebspqckawkhbrtlqwblfwzfptanhiglaabjea", + ), + true, + ), + (("abccef", "cgdhga"), false), + ( + ( + "alcojrucnipkxjrwvuvnwraixyndxzfbwgyuaohesyciodlukfbbqkprcwkxumumuxkwcrpkqbbfkuldoicysehoauygwbfzxdnyxiarwnvuvwrrjcyrltbetaahm", + "mhaatebtlrycjrvzmboowswllwvupbbcxplmrtlhpcnkulvmaaroopdklzfaegvagiknaowcjkemvaiaysmrcudmmbofzjccrixztupmzuvupzjpoloebmfmeoeqo" + ), + true, + ), + ]; + + for ((a, b), expected) in test_cases { + assert_eq!(S::check_palindrome_formation(a.to_string(), b.to_string()), expected); + } + } +}