#K92812. String Compression
String Compression
String Compression
You are given a non-empty string s
. Your task is to compress the string by replacing sequences of the same character with the character followed by the count of repetitions. Specifically, if a character c
appears consecutively k times, it should be replaced by ck
. For example, the string "aabcccccaaa" becomes "a2b1c5a3".
If the compressed string is not strictly shorter than the original string, output the original string instead.
Note: The compression should only be applied if it reduces the overall length. Otherwise, the original string is considered the best representation.
Input/Output: The input is read from stdin and the output should be printed to stdout.
inputFormat
The input consists of a single line containing the string s
(which may be empty). The string will only contain lowercase English letters.
outputFormat
Print the compressed string if its length is strictly shorter than the original string, otherwise print the original string.
## sampleaabcccccaaa
a2b1c5a3