using System.Text.RegularExpressions;
public static void Main()
var test1 = "aaabccccdd";
var test2 = "yyyyyzzzzzzzzzz";
var test4 = "hhhccchhhccc";
Console.WriteLine(ProcessStringCompression(test1));
Console.WriteLine(ProcessStringCompression(test2));
Console.WriteLine(ProcessStringCompression(test3));
Console.WriteLine(ProcessStringCompression(test4));
public static string ProcessStringCompression(string stringToCompress)
if (stringToCompress == null)
throw new ArgumentNullException("stringToCompress");
if (!Regex.IsMatch(stringToCompress, @"^[a-zA-Z]+$"))
throw new ArgumentException("Only non numeric alpha characters supported.");
if (stringToCompress.Length < 2)
var charArray = stringToCompress.ToCharArray();
while (i < charArray.Length)
while (j < charArray.Length && charArray[j] == charArray[i])
charArray[indexAt] = charArray[i];
var countAsString = counter.ToString();
foreach (var theChar in countAsString)
charArray[++indexAt] = theChar;
return new string (charArray, 0, indexAt);