-
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 1754: Largest Merge Of Two Strings
- Loading branch information
Showing
3 changed files
with
76 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
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,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::<super::Solution>(); | ||
} | ||
} |
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,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<S: Solution>() { | ||
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); | ||
} | ||
} | ||
} |