leetcode

Leetcode problem solutions
git clone https://git.sinitax.com/sinitax/leetcode
Log | Files | Refs | sfeed.txt

1768-merge-strings-alternately.rs (2390B)


      1/// 1768. Merge Strings Alternately (Easy)
      2///
      3/// You are given two strings word1 and word2. Merge the strings by adding
      4/// letters in alternating order, starting with word1. If a string is longer
      5/// than the other, append the additional letters onto the end of the merged
      6/// string.
      7///
      8/// Return the merged string.
      9///
     10/// *Example 1:*
     11///
     12///   *Input:* word1 = "abc", word2 = "pqr"
     13///   *Output:* "apbqcr"
     14///   *Explanation:* The merged string will be merged as so:
     15///   word1:  a   b   c
     16///   word2:    p   q   r
     17///   merged: a p b q c r
     18///
     19/// *Example 2:*
     20///
     21///   *Input:* word1 = "ab", word2 = "pqrs"
     22///   *Output:* "apbqrs"
     23///   *Explanation:* Notice that as word2 is longer, "rs" is appended to the end.
     24///   word1:  a   b
     25///   word2:    p   q   r   s
     26///   merged: a p b q   r   s
     27///
     28/// *Example 3:*
     29///
     30///   *Input:* word1 = "abcd", word2 = "pq"
     31///   *Output:* "apbqcd"
     32///   *Explanation:* Notice that as word1 is longer, "cd" is appended to the end.
     33///   word1:  a   b   c   d
     34///   word2:    p   q
     35///   merged: a p b q c   d
     36///
     37/// *Constraints:*
     38///
     39///   * 1 <= word1.length, word2.length <= 100
     40///   * word1 and word2 consist of lowercase English letters.
     41///
     42use leetcode::*;
     43
     44struct Solution {}
     45
     46impl Solution {
     47    pub fn merge_alternately(word1: String, word2: String) -> String {
     48        let mut out = Vec::with_capacity(word1.len() + word2.len());
     49        let mut word1 = word1.chars().peekable();
     50        let mut word2 = word2.chars().peekable();
     51        while word1.peek().is_some() || word2.peek().is_some() {
     52            if let Some(c) = word1.next() {
     53                out.push(c);
     54            }
     55            if let Some(c) = word2.next() {
     56                out.push(c)
     57            }
     58        }
     59        out.into_iter().collect()
     60    }
     61}
     62
     63pub fn main() {
     64    println!("{}", Solution::merge_alternately(arg(1), arg(2)))
     65}
     66
     67#[cfg(test)]
     68mod tests {
     69    use crate::Solution;
     70
     71    #[test]
     72    fn test() {
     73        assert_eq!(
     74            Solution::merge_alternately("abc".to_string(), "pqr".to_string()),
     75            "apbqcr".to_string()
     76        );
     77        assert_eq!(
     78            Solution::merge_alternately("ab".to_string(), "pqrs".to_string()),
     79            "apbqrs".to_string()
     80        );
     81        assert_eq!(
     82            Solution::merge_alternately("abcd".to_string(), "pq".to_string()),
     83            "apbqcd".to_string()
     84        );
     85    }
     86}