-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the solution. LeetCode #2109 'Adding Spaces to a String' [Med…
…ium].
- Loading branch information
1 parent
a6a5768
commit d508b0f
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...Code/src/LeetCode.Challenges/Problems21xx/N_2109_AddingSpacesToString/ImprovedSolution.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...de.Challenges.UnitTests/Problems21xx/N_2109_AddingSpacesToString/ImprovedSolutionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |