From e11181744ea1f1918d6849dfdde48626055ea79e Mon Sep 17 00:00:00 2001 From: EFanZh Date: Mon, 8 Jan 2024 22:17:31 +0800 Subject: [PATCH] Add problem 1754: Largest Merge Of Two Strings --- src/lib.rs | 1 + .../greedy.rs | 54 +++++++++++++++++++ .../mod.rs | 21 ++++++++ 3 files changed, 76 insertions(+) create mode 100644 src/problem_1754_largest_merge_of_two_strings/greedy.rs create mode 100644 src/problem_1754_largest_merge_of_two_strings/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 5e986b93..b1734b43 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1308,6 +1308,7 @@ pub mod problem_1749_maximum_absolute_sum_of_any_subarray; pub mod problem_1750_minimum_length_of_string_after_deleting_similar_ends; pub mod problem_1752_check_if_array_is_sorted_and_rotated; pub mod problem_1753_maximum_score_from_removing_stones; +pub mod problem_1754_largest_merge_of_two_strings; pub mod problem_1758_minimum_changes_to_make_alternating_binary_string; pub mod problem_1759_count_number_of_homogenous_substrings; pub mod problem_1760_minimum_limit_of_balls_in_a_bag; diff --git a/src/problem_1754_largest_merge_of_two_strings/greedy.rs b/src/problem_1754_largest_merge_of_two_strings/greedy.rs new file mode 100644 index 00000000..3ba9859b --- /dev/null +++ b/src/problem_1754_largest_merge_of_two_strings/greedy.rs @@ -0,0 +1,54 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn largest_merge(word1: String, word2: String) -> String { + let mut iter_1 = word1.as_bytes().iter(); + let mut iter_2 = word2.as_bytes().iter(); + let mut c1 = *iter_1.next().unwrap(); + let mut result = Vec::with_capacity(word1.len() + word2.len()); + + 'outer: while let Some(&c2) = iter_2.next() { + loop { + if (c2, iter_2.as_slice()) > (c1, iter_1.as_slice()) { + result.push(c2); + + break; + } + + result.push(c1); + + if let Some(&next_c1) = iter_1.next() { + c1 = next_c1; + } else { + c1 = c2; + iter_1 = iter_2; + + break 'outer; + } + } + } + + result.push(c1); + result.extend(iter_1); + + String::from_utf8(result).unwrap() + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn largest_merge(word1: String, word2: String) -> String { + Self::largest_merge(word1, word2) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_1754_largest_merge_of_two_strings/mod.rs b/src/problem_1754_largest_merge_of_two_strings/mod.rs new file mode 100644 index 00000000..26da1dec --- /dev/null +++ b/src/problem_1754_largest_merge_of_two_strings/mod.rs @@ -0,0 +1,21 @@ +pub mod greedy; + +pub trait Solution { + fn largest_merge(word1: String, word2: String) -> String; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + (("cabaa", "bcaaa"), "cbcabaaaaa"), + (("abcabc", "abdcaba"), "abdcabcabcaba"), + ]; + + for ((word1, word2), expected) in test_cases { + assert_eq!(S::largest_merge(word1.to_string(), word2.to_string()), expected); + } + } +}