#C12228. Run-Length Encoding String Compression
Run-Length Encoding String Compression
Run-Length Encoding String Compression
In this problem, you are required to compress a given string using the Run-Length Encoding (RLE) algorithm. For a string ( s ), replace consecutive groups of the same character with the character followed by the number of times it appears consecutively. Formally, if a character ( c ) appears ( k ) times consecutively, it is replaced with ( c,k ) in the compressed string. However, if the compressed string is not strictly shorter than the original string, you must return the original string.
For example, given ( s = \text{'aabcccccaaa'} ), the expected output is ( \text{'a2b1c5a3'} ) because it is shorter than the original string. On the other hand, for ( s = \text{'abcdefgh'} ), compressing it would yield ( \text{'a1b1c1d1e1f1g1h1'} ) which is longer than the original string, so the output should be ( \text{'abcdefgh'} ).
inputFormat
The input consists of a single line containing the string ( s ) that you need to compress. The string can be empty and does not contain any spaces.
outputFormat
Output a single line containing the compressed string if it is strictly shorter than the original string; otherwise, output the original string.## sample
aabcccccaaa
a2b1c5a3