← All coding problems
Run-Length Encoding
easyPlacement Coding+10 XPTime limit 2s
Compress a string with run-length encoding: replace every block of the same character repeated one after another by the character followed by the length of the block. aaabccdddd becomes a3b1c2d4.
Input: one line with a string of lowercase letters.
Output: the encoded string.
Constraints: 1 ≤ length ≤ 10⁵.
Think about it: walk through the string with a pointer i; move a second pointer forward while the character stays the same. The distance between them is the count.
Sample input 1
aaabccdddd
Sample output 1
a3b1c2d4
Sample input 2
abc
Sample output 2
a1b1c1
Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).