-
Notifications
You must be signed in to change notification settings - Fork 0
/
1768-Merge-Strings-Alternately.cpp
49 lines (41 loc) · 1.35 KB
/
1768-Merge-Strings-Alternately.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
// Author : Vicen-te
// Date : 09/27/2023 (MM-DD-YYYY)
/*
Description:
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.
Return the merged string.
Ex1:
Input: word1 = "abc", word2 = "pqr"
Output: "apbqcr"
Explanation: The merged string will be merged as so:
word1: a b c
word2: p q r
merged: a p b q c r
Algorithm:
Append each character from both words (word1, word2) to a new string variable.
While the loop iterates, ensure that each word (word1, word2) remains within its length;
otherwise, an error will occur.
Time Complexity: O(N)
Space Complexity: O(N)
*/
class Solution {
public:
string mergeAlternately(string word1, string word2) {
string result = "";
for(int i = 0; i < word1.length() || i < word2.length(); ++i)
{
if(i < word1.length())
{
result += word1[i];
}
if(i < word2.length())
{
result += word2[i];
}
}
return result;
}
};