Run-Length Compression

This technique is called RLE compression: consecutive repeated characters are replaced by the character and the number of times it appears. For example, aaaabbcc becomes a4b2c2. Write a function compress that compresses the string s using this rule. If the compressed result is not shorter than the original string, return the original string unchanged.

Примеры

Ввод
"aaaabbcc"
Вывод
"a4b2c2"
Ввод
"abc"
Вывод
"abc"
Ввод
"aabcccccaaa"
Вывод
"a2b1c5a3"

Нужно реализовать

def compress(s: str) -> str:

Теги

stringstwo-pointers
PostgreSQLv16

Här visas resultatet av din kod

Visible tests
1
Input
"aaaabbcc"
Expected"a4b2c2"
2
Input
"abc"
Expected"abc"
3
Input
"aabcccccaaa"
Expected"a2b1c5a3"
4
Input
""
Expected""
5
Input
"aaaa"
Expected"a4"
6
Input
"AAAA"
Expected"A4"
Focus radio
Paused · SomaFM · Fluid