Skip to content

Commit

Permalink
Improve the solution. LeetCode #2109 'Adding Spaces to a String' [Med…
Browse files Browse the repository at this point in the history
…ium].
  • Loading branch information
eminencegrs committed Dec 3, 2024
1 parent a6a5768 commit d508b0f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Text;

namespace LeetCode.Challenges.Problems21xx.N_2109_AddingSpacesToString;

public static class ImprovedSolution
{
public static string AddSpaces(string input, int[] spaces)
{
var sb = new StringBuilder();
var charPointer = 0;
var spacePointer = 0;

while (charPointer < input.Length)
{
if (spacePointer < spaces.Length && charPointer == spaces[spacePointer])
{
sb.Append(' ');
spacePointer++;
}

sb.Append(input[charPointer]);
charPointer++;
}

return sb.ToString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using LeetCode.Challenges.Problems21xx.N_2109_AddingSpacesToString;
using Shouldly;
using Xunit;

namespace LeetCode.Challenges.UnitTests.Problems21xx.N_2109_AddingSpacesToString;

public class ImprovedSolutionTests
{
[Theory]
[ClassData(typeof(TestData))]
public void GivenStringAndSpaces_WhenMinCapability_ThenResultAsExpected(
string input, int[] spaces, string expectedResult)
{
ImprovedSolution.AddSpaces(input, spaces).ShouldBeEquivalentTo(expectedResult);
}
}

0 comments on commit d508b0f

Please sign in to comment.