| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | using System.Text; |
| | 13 | |
|
| | 14 | | namespace LeetCode.Algorithms.MergeStringsAlternately; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class MergeStringsAlternatelyOnePointer : IMergeStringsAlternately |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(m + n) |
| | 21 | | /// Space complexity - O(1) |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="word1"></param> |
| | 24 | | /// <param name="word2"></param> |
| | 25 | | /// <returns></returns> |
| | 26 | | public string MergeAlternately(string word1, string word2) |
| 3 | 27 | | { |
| 3 | 28 | | var resultStringBuilder = new StringBuilder(); |
| | 29 | |
|
| 3 | 30 | | var i = 0; |
| | 31 | |
|
| 14 | 32 | | while (i < word1.Length || i < word2.Length) |
| 11 | 33 | | { |
| 11 | 34 | | if (i < word1.Length) |
| 9 | 35 | | { |
| 9 | 36 | | resultStringBuilder.Append(word1[i]); |
| 9 | 37 | | } |
| | 38 | |
|
| 11 | 39 | | if (i < word2.Length) |
| 9 | 40 | | { |
| 9 | 41 | | resultStringBuilder.Append(word2[i]); |
| 9 | 42 | | } |
| | 43 | |
|
| 11 | 44 | | i++; |
| 11 | 45 | | } |
| | 46 | |
|
| 3 | 47 | | return resultStringBuilder.ToString(); |
| 3 | 48 | | } |
| | 49 | | } |