-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp1768.rs
36 lines (30 loc) · 1.01 KB
/
p1768.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let word1_1: String = "abc".to_string();
let word2_1: String = "pqr".to_string();
assert_eq!(merge_alternately(word1_1, word2_1), "apbqcr".to_string());
let word1_2: String = "ab".to_string();
let word2_2: String = "pqrs".to_string();
assert_eq!(merge_alternately(word1_2, word2_2), "apbqrs".to_string());
let word1_3: String = "abcd".to_string();
let word2_3: String = "pq".to_string();
assert_eq!(merge_alternately(word1_3, word2_3), "apbqcd".to_string());
}
}
pub fn merge_alternately(word1: String, word2: String) -> String {
let word1: Vec<char> = word1.chars().collect();
let word2: Vec<char> = word2.chars().collect();
let mut ans: String = String::new();
for i in 0..word1.len().max(word2.len()) {
if i < word1.len() {
ans.push(word1[i]);
}
if i < word2.len() {
ans.push(word2[i]);
}
}
ans
}