Skip to content

Instantly share code, notes, and snippets.

@unilecs
Created December 10, 2024 06:11
Show Gist options
  • Save unilecs/cd637d92c56f5f77e2b9f6caa924b23e to your computer and use it in GitHub Desktop.
Save unilecs/cd637d92c56f5f77e2b9f6caa924b23e to your computer and use it in GitHub Desktop.
Задача: Компрессия строки
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"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment