Skip to content

Commit

Permalink
Add problem 1616: Split Two Strings to Make Palindrome
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Sep 16, 2023
1 parent f411c42 commit f26f92e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 59 additions & 0 deletions src/problem_1616_split_two_strings_to_make_palindrome/greedy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
fn is_palindrome(mut iter: impl DoubleEndedIterator<Item = u8>) -> 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::<super::Solution>();
}
}
38 changes: 38 additions & 0 deletions src/problem_1616_split_two_strings_to_make_palindrome/mod.rs
Original file line number Diff line number Diff line change
@@ -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<S: Solution>() {
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);
}
}
}

0 comments on commit f26f92e

Please sign in to comment.