-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1616: Split Two Strings to Make Palindrome
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/problem_1616_split_two_strings_to_make_palindrome/greedy.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
src/problem_1616_split_two_strings_to_make_palindrome/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |