Compress Repeated Runs
Run-length encoding is useful when a string has many identical characters in a row: instead of storing the whole run, you store its length and the character. The function rle_encode receives a string s and replaces each consecutive run of the same character with the number of repetitions followed by that character. For example, aaabbc becomes 3a2b1c.
Примеры
Ввод
"aaabbc"Вывод
"3a2b1c"Ввод
"aaaaa"Вывод
"5a"Ввод
"abc"Вывод
"1a1b1c"Нужно реализовать
def rle_encode(s: str) -> str:
Теги
strings
Log in om je inzendingsgeschiedenis te zien
InloggenLog in om de AI-mentor te gebruiken
InloggenVisible tests
1
Input
"aaabbc"Expected
"3a2b1c"2
Input
"aaaaa"Expected
"5a"3
Input
"abc"Expected
"1a1b1c"4
Input
""Expected
""5
Input
"WWWWWWWWWWWWBWWWWWWWWWWWWBBB"Expected
"12W1B12W3B"6
Input
"a"Expected
"1a"Focus radio
Paused · SomaFM · Fluid