Skip to content
Snippets Groups Projects
Commit efa9f4ee authored by Dmytro Bogatov's avatar Dmytro Bogatov :two_hearts:
Browse files

Cahnges overview:

Add StringCompression.
parent abdef015
Branches
No related tags found
No related merge requests found
......@@ -37,6 +37,7 @@
<Compile Include="Sort\MergeSortTests.cs" />
<Compile Include="Sort\QuickSortTests.cs" />
<Compile Include="Sort\HeapSortTests.cs" />
<Compile Include="CrackingTheCodingInterview\DataStructures\StringCompressionTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
......@@ -49,6 +50,8 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Sort\" />
<Folder Include="CrackingTheCodingInterview\" />
<Folder Include="CrackingTheCodingInterview\DataStructures\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file
//
//
// This file - StringCompressionTests.cs was created by Dmytro Bogatov (dmytro@dbogatov.org)
// on 12/7/2016, 3:47 PM
using NUnit.Framework;
namespace CrackingTheCodingInterview.DataStructures
{
[TestFixture]
public class StringCompressionTests
{
[Test]
public void TestCases()
{
StringAssert.AreEqualIgnoringCase("a2b1c5a3", StringCompression.Compress("aabcccccaaa"));
StringAssert.AreEqualIgnoringCase("abcd", StringCompression.Compress("abcd"));
StringAssert.AreEqualIgnoringCase("", StringCompression.Compress(""));
StringAssert.AreEqualIgnoringCase("a", StringCompression.Compress("a"));
StringAssert.AreEqualIgnoringCase("aaabcdefgh", StringCompression.Compress("aaabcdefgh"));
}
}
}
......@@ -37,9 +37,12 @@
<Compile Include="Sort\MergeSort.cs" />
<Compile Include="Sort\QuickSort.cs" />
<Compile Include="Sort\HeapSort.cs" />
<Compile Include="CrackingTheCodingInterview\DataStructures\StringCompression.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Sort\" />
<Folder Include="CrackingTheCodingInterview\" />
<Folder Include="CrackingTheCodingInterview\DataStructures\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file
//
//
// This file - StringCompression.cs was created by Dmytro Bogatov (dmytro@dbogatov.org)
// on 12/7/2016, 3:44 PM
/*
* Implement a method to perform basic string compression using the counts of repeated characters.
* For example, the string aabcccccaaa would become a2blc5a3.
* If the "compressed" string would not become smaller than the original string,
* your method should return the original string.
*/
namespace CrackingTheCodingInterview.DataStructures
{
public class StringCompression
{
public static string Compress(string input)
{
if (input.Length < 2)
{
return input;
}
string output = "";
var current = input[0];
var count = 1;
for (int i = 1; i < input.Length; i++)
{
var newChar = input[i];
if (newChar != current)
{
output += current + count.ToString();
current = newChar;
count = 1;
}
else
{
count++;
}
}
output += current + count.ToString();
return output.Length < input.Length ? output : input;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment