Menu
CoddyTech

String Compression

You get a string s of English letters. Compress it: replace every run of two or more equal letters in a row with that letter followed by the length of the run, and leave a letter that stands alone as it is. So aabccc becomes a2bc3. Return the compressed string.

Upper and lower case are different letters: aA is two runs of length 1.

Function

compress(s: string) → string
sstring
the string to compress, English letters only
Returnsstring
the compressed string, each run written as its letter plus its length when the length is more than 1

Constraints

  • 1 ≤ s.length ≤ 2 × 104
  • s contains only English letters, a to z and A to Z.
  • A run length is written in decimal and can have several digits.

Examples

Input
s = "aabccc"
Output
"a2bc3"
Explanation
The runs are aa, b and ccc. They become a2, b (a lone letter gets no count) and c3.
challenge icon

Follow-up

Write the reverse: given a compressed string such as ab12, rebuild the original. How do you read a count that has several digits?

Reset code
def compress(s):
    # Write code here

Test #1

Test #2

Test #3

Argument

s = "aabccc"

Expected Output

"a2bc3"