cs: add “1768. Merge Strings Alternately”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
77096af698
commit
641aa6c529
1 changed files with 20 additions and 0 deletions
20
cs/merge-strings-alternately.cs
Normal file
20
cs/merge-strings-alternately.cs
Normal file
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue