-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathMerge_string_alternatively.cpp
81 lines (64 loc) · 2.15 KB
/
Merge_string_alternatively.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
Problem:
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.
Input Format:
The first line consists of an integer T which denotes the number of test cases.
For each test case we enter two strings to be merged alternatively
Output Format:
For each test case we get a alternatively merged string.
Explanation:
First we alternatively add both the strings to the resultant string till the length of the smallest string (both strings can also be equal length).
After that we add the substring of the remaining longer string to resultant string if the length of both strings are not equal.
*/
#include<bits/stdc++.h>
using namespace std;
/* A function that will take 2 string as input and return the resultant merged string formed alternatively from the inputted strings */
string mergeAlternately(string word1, string word2) {
string ans="";
/* A variable that will take the size of the smallest string among word1 and word2 */
int less=min(word1.length(),word2.length());
int i=0,j=0;
/* Loop to alternatively add word by word from each string to resultant string */
while(i<less || j<less){
if(j>=i){
ans+=word1[i];
i++;
}
else{
ans+=word2[j];
j++;
}
}
/* If both strings are of unequal sizes then add the rest of substring to the resultant string */
if(word1.length()!=word2.length()){
if(word1.length()>less){
ans+=word1.substr(less,word1.length());
}
else{
ans+=word2.substr(less,word2.length());
}
}
return ans;
}
int main(){
int t;
cin>>t;
while(t--){
string word1,word2;
cin>>word1>>word2;
cout<<mergeAlternately(word1,word2)<<endl;
}
}
/*
Time complexity: O(N) where N is the length of the longest string.
Space complexity: O(M) where M is the summation of length of both strings.
Sample input:
2
abc pqr
abcd pq
Sample Output:
apbqcr
apbqcd
*/