-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution1768.cs
37 lines (31 loc) · 962 Bytes
/
Solution1768.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
using System.Text;
namespace LeetCode.Solutions;
public class Solution1768
{
/// <summary>
/// 1768. Merge Strings Alternately - Easy
/// <a href="https://leetcode.com/problems/merge-strings-alternately">See the problem</a>
/// </summary>
public string MergeAlternately(string word1, string word2)
{
var i = 0;
var strBuilder = new StringBuilder();
// Append the first i characters from each string
while (i < word1.Length && i < word2.Length)
{
strBuilder.Append(word1[i]);
strBuilder.Append(word2[i]);
i++;
}
// Append the rest of the longer string
for (var j = i; j < word2.Length; j++)
{
strBuilder.Append(word2[j]);
}
for (var j = i; j < word1.Length; j++)
{
strBuilder.Append(word1[j]);
}
return strBuilder.ToString();
}
}