:w (1898B)
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/// 42 43use leetcode::*; 44 45struct Solution {} 46 47impl Solution { 48 pub fn merge_alternately(word1: String, word2: String) -> String { 49 let mut out = Vec::with_capacity(word1.len() + word2.len()); 50 let mut word1 = word1.chars().peekable(); 51 let mut word2 = word2.chars().peekable(); 52 while word1.peek().is_some() || word2.peek().is_some() { 53 if let Some(c) = word1.next() { 54 out.push(c); 55 } 56 if let Some(c) = word2.next() { 57 out.push(c) 58 } 59 } 60 out.into_iter().collect() 61 } 62} 63 64pub fn main() { 65} 66 67#[cfg(test)] 68mod tests { 69 use crate::Solution; 70 71 #[test] 72 fn test() { 73 } 74}