Skip to content

Commit 3369b6b

Browse files
committed
1768. Merge Strings Alternately: AC
1 parent 88e8bf1 commit 3369b6b

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,3 +1332,4 @@ mod s1763_longest_nice_substring;
13321332
mod s1764_form_array_by_concatenating_subarrays_of_another_array;
13331333
mod s1765_map_of_highest_peak;
13341334
mod s1766_tree_of_coprimes;
1335+
mod s1768_merge_strings_alternately;
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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

Comments
 (0)