|
| 1 | +/** |
| 2 | + * [1768] Merge Strings Alternately |
| 3 | + * |
| 4 | + * You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string. |
| 5 | + * |
| 6 | + * Return the merged string. |
| 7 | + * |
| 8 | + * |
| 9 | + * Example 1: |
| 10 | + * |
| 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 | + * |
| 20 | + * Example 2: |
| 21 | + * |
| 22 | + * |
| 23 | + * Input: word1 = "ab", word2 = "pqrs" |
| 24 | + * Output: "apbqrs" |
| 25 | + * Explanation: Notice that as word2 is longer, "rs" is appended to the end. |
| 26 | + * word1: a b |
| 27 | + * word2: p q r s |
| 28 | + * merged: a p b q r s |
| 29 | + * |
| 30 | + * |
| 31 | + * Example 3: |
| 32 | + * |
| 33 | + * |
| 34 | + * Input: word1 = "abcd", word2 = "pq" |
| 35 | + * Output: "apbqcd" |
| 36 | + * Explanation: Notice that as word1 is longer, "cd" is appended to the end. |
| 37 | + * word1: a b c d |
| 38 | + * word2: p q |
| 39 | + * merged: a p b q c d |
| 40 | + * |
| 41 | + * |
| 42 | + * |
| 43 | + * Constraints: |
| 44 | + * |
| 45 | + * |
| 46 | + * 1 <= word1.length, word2.length <= 100 |
| 47 | + * word1 and word2 consist of lowercase English letters. |
| 48 | + * |
| 49 | + */ |
| 50 | +pub struct Solution {} |
| 51 | + |
| 52 | +// problem: https://leetcode.com/problems/merge-strings-alternately/ |
| 53 | +// discuss: https://leetcode.com/problems/merge-strings-alternately/discuss/?currentPage=1&orderBy=most_votes&query= |
| 54 | + |
| 55 | +// submission codes start here |
| 56 | + |
| 57 | +impl Solution { |
| 58 | + pub fn merge_alternately(word1: String, word2: String) -> String { |
| 59 | + let mut chars1 = word1.chars(); |
| 60 | + let mut chars2 = word2.chars(); |
| 61 | + |
| 62 | + let common_len = word1.len().min(word2.len()); |
| 63 | + |
| 64 | + let zipped = std::iter::zip( |
| 65 | + chars1.by_ref().take(common_len), |
| 66 | + chars2.by_ref().take(common_len), |
| 67 | + ); |
| 68 | + |
| 69 | + let mut result = String::with_capacity(word1.len() + word2.len()); |
| 70 | + zipped.for_each(|(a, b)| { |
| 71 | + result.push(a); |
| 72 | + result.push(b) |
| 73 | + }); |
| 74 | + |
| 75 | + result.extend(chars1); |
| 76 | + result.extend(chars2); |
| 77 | + |
| 78 | + result |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// submission codes end |
| 83 | + |
| 84 | +#[cfg(test)] |
| 85 | +mod tests { |
| 86 | + use super::*; |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn test_1768_example_1() { |
| 90 | + let word1 = "abc".to_string(); |
| 91 | + let word2 = "pqr".to_string(); |
| 92 | + |
| 93 | + let result = "apbqcr".to_string(); |
| 94 | + |
| 95 | + assert_eq!(Solution::merge_alternately(word1, word2), result); |
| 96 | + } |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn test_1768_example_2() { |
| 100 | + let word1 = "ab".to_string(); |
| 101 | + let word2 = "pqrs".to_string(); |
| 102 | + |
| 103 | + let result = "apbqrs".to_string(); |
| 104 | + |
| 105 | + assert_eq!(Solution::merge_alternately(word1, word2), result); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn test_1768_example_3() { |
| 110 | + let word1 = "abcd".to_string(); |
| 111 | + let word2 = "pq".to_string(); |
| 112 | + |
| 113 | + let result = "apbqcd".to_string(); |
| 114 | + |
| 115 | + assert_eq!(Solution::merge_alternately(word1, word2), result); |
| 116 | + } |
| 117 | +} |
0 commit comments