| | 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.AddingSpacesToString; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class AddingSpacesToStringStringBuilder : IAddingSpacesToString |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(n + m) |
| | 21 | | /// Space complexity - O(n + m) |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="s"></param> |
| | 24 | | /// <param name="spaces"></param> |
| | 25 | | /// <returns></returns> |
| | 26 | | public string AddSpaces(string s, int[] spaces) |
| 3 | 27 | | { |
| 3 | 28 | | var stringBuilder = new StringBuilder(); |
| | 29 | |
|
| 3 | 30 | | var spaceIndex = 0; |
| | 31 | |
|
| 86 | 32 | | for (var i = 0; i < s.Length; i++) |
| 40 | 33 | | { |
| 40 | 34 | | if (spaceIndex < spaces.Length && i == spaces[spaceIndex]) |
| 14 | 35 | | { |
| 14 | 36 | | stringBuilder.Append(' '); |
| | 37 | |
|
| 14 | 38 | | spaceIndex++; |
| 14 | 39 | | } |
| | 40 | |
|
| 40 | 41 | | stringBuilder.Append(s[i]); |
| 40 | 42 | | } |
| | 43 | |
|
| 3 | 44 | | return stringBuilder.ToString(); |
| 3 | 45 | | } |
| | 46 | | } |