Run-Length Encoding
Riverbend Textiles writes weaving patterns as a row of letters, one letter per stitch, each letter a colour. Written out in full a row is unreadable at the loom, so the pattern book records it compactly: each stretch of the same colour standing next to each other becomes the colour followed by how many stitches it covers. A weaver reading the book has to be able to rebuild the row exactly, so no stretch may be left out and none may be merged with another.
Task: Print the compact form of the row.
Input
A single line holding between 1 and 1000 lowercase letters, with no spaces.
Output
One line. Read the row from left to right; every stretch of identical letters standing next to each other is written as that letter followed immediately by the number of stitches in that stretch. A stretch of one is still written with its 1. Nothing separates one stretch from the next.
Example:
Input:
aaabbc
Output:
a3b2c1
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in