-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeStringsAlternately.cs
44 lines (37 loc) · 1.25 KB
/
MergeStringsAlternately.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LeetCodeSolutions
{
// Source: https://leetcode.com/problems/merge-strings-alternately/
// Author: Jalal Shabo
// Date : 11/17/2023
// Time Complexity: O(n)
// Space Complexity: O(1)
public static class MergeStringsAlternately
{
public static string MergeStringsAlternatelySolution(string word1, string word2){
StringBuilder mergedAlternately = new StringBuilder();
int counter = 0;
// look through both and append, after which append end of which ever list has remainder
while (counter < word1.Length && counter < word2.Length )
{
mergedAlternately.Append(word1[counter]).Append(word2[counter]);
counter++;
}
while (counter < word1.Length )
{
mergedAlternately.Append(word1[counter]);
counter++;
}
while (counter < word2.Length )
{
mergedAlternately.Append(word2[counter]);
counter++;
}
return mergedAlternately.ToString();
}
}
}