Created
December 10, 2024 06:11
-
-
Save unilecs/cd637d92c56f5f77e2b9f6caa924b23e to your computer and use it in GitHub Desktop.
Задача: Компрессия строки
This file contains hidden or 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
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