This question already has an answer here:
- Run-Length Encoding [duplicate] 19 answers
Run-length encoding — sequences in which the same data value occurs in many consecutive data elements are stored as a single data value and count
When using run-length encoding:
Input: ABBBCCACB Output: 1A3B2C1A1C1B
This sample has an output that is larger than the input.
Instead, it can be simplified for binary inputs by removing the data value in the result,
Input: 10001110101 Output: 1331111
which is significantly smaller. This works because, logically, if the there is a "next subset" then it must be of a different data value than the current subset. Since there are only two possible values (1 and 0), you always know the "next" data value subset.
Implement binary run-length encoding demonstrated above.
Take input through STDIN or suitable alternative.
Output to STDOUT or suitable alternative.
In: 101011101011000011 Out: 11113111242 In: 01001010001 Out: 11211131 In: 000000000 Out: 9
You may assume that no subsets will be longer than 9 characters (there won't be 10 adjacent zeroes).