From 641aa6c529b2857a7d5b2a4ac63999f1abab2c72 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 6 Jan 2024 22:04:18 +0100 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=E2=80=9C1768.=20Merge=20Strings=20?= =?UTF-8?q?Alternately=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- cs/merge-strings-alternately.cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 cs/merge-strings-alternately.cs diff --git a/cs/merge-strings-alternately.cs b/cs/merge-strings-alternately.cs new file mode 100644 index 0000000..ac8a237 --- /dev/null +++ b/cs/merge-strings-alternately.cs @@ -0,0 +1,20 @@ +public class Solution { + public string MergeAlternately(string word1, string word2) { + var sb = new StringBuilder(); + + int i, j; + for (i = 0, j = 0; i < word1.Length && j < word2.Length; ++i, ++j) { + sb.Append(word1[i]); + sb.Append(word2[j]); + } + + if (i < word1.Length) { + sb.Append(word1.Substring(i)); + } + if (j < word2.Length) { + sb.Append(word2.Substring(j)); + } + + return sb.ToString(); + } +}