#K9691. Run-Length Encoding Compression
Run-Length Encoding Compression
Run-Length Encoding Compression
You are given a string s
. Your task is to compress the string using a simple run-length encoding algorithm:
- If a character appears consecutively more than once, replace the consecutive block with the character followed by the count of its repetitions.
- If a character appears only once, it remains unchanged.
After compressing the string, compare its length with the original string. If the compressed string is strictly shorter than the original string, output the compressed string; otherwise, output the original string.
For example:
aaabbccccc
is compressed toa3b2c5
.abcd
remainsabcd
because the compressed version would not reduce the length.
Ensure that your solution reads input from stdin and writes output to stdout.
inputFormat
The input consists of a single line containing a non-empty string s
. The string may be empty as well. Read the string from stdin.
outputFormat
Output the compressed string (or the original string if the compressed version is not shorter) to stdout.
## sampleaaabbccccc
a3b2c5