Run-length encoding

Shrink a string by replacing runs of the same character with a count.

What to build

A simple form of compression, in which each run becomes a letter and a number.

Requirements

  • Read a word.
  • For each run of identical characters in a row, output the character and then the length of the run.

Input

One word.

Output

One line.

Constraints

The word has from 1 to 1000 lowercase letters.

Example

Input:

aaabcc

Output:

a3b1c2

Why it works

Only neighbouring characters matter, so one pass through the word with one counter is enough. The final run needs its own step, because no change of character comes after it.

Examples

Example 1 · Input
aaabcc
Expected output
a3b1c2
Example 2 · Input
a
Expected output
a1

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Remember the current character and how many times you have seen it in a row.

Hint 2

When the character changes, add the finished run to the result.

Hint 3

Do not forget the last run, after the loop ends.

Solution

Show one correct solution (try it yourself first)
word = input()
result = ""
current = word[0]
count = 0
for ch in word:
    if ch == current:
        count += 1
    else:
        result += current + str(count)
        current = ch
        count = 1
result += current + str(count)
print(result)

Lesson: Working with strings →