pyassistant

Run-length encode a string

Implement a simple run-length encoding for a string. Given a string s, compress consecutive repeated characters by replacing each run with the character followed by the run length (as a decimal number). If a character appears only once consecutively, include just the character with no number. Examples: - "aaabbc" -> "a3b2c" - "abc" -> "abc" - "zzzzzz" -> "z6" Write a function solve(s: str) -> str that returns the compressed string. The input string may be empty; in that case return an empty string. The function should preserve the original order and be case-sensitive ("A" and "a" are different).

Example:

Input:
('aaabbc',)
Output:
'a3b2c'

Make sure you return your solution, don't print!

AI

Bot

Trying to solve my challenge? Ask if you must, or press the purple button so I can analyze your code.