CompressedStr.cs

CSharp · Console

/Program.cs

using System;
using System.Text;
					
public class Program
{
	public static string CompressedStr(string str) {
        var res = new StringBuilder();
        int index = 0;

        while (index < str.Length)
        {
            int count = 0;
            var curr = str[index];

            while(index < str.Length && count < 9 && str[index] == curr) {
                count++;
                index++;
            }

            res.Append(count).Append(curr);
        }

        return res.ToString();
    }
	
	public static void Main()
	{
		Console.WriteLine("UniLecs");
		
		// tests
		Console.WriteLine(CompressedStr("abcde")); // "1a1b1c1d1e"
		Console.WriteLine(CompressedStr("aaaaaaaaaaaaaabb")); // "9a5a2b"
	}
}
;