#K6601. String Compression
String Compression
String Compression
You are given a string s. Your task is to compress the string using the following rules:
- Consecutive identical characters (letters or digits) are replaced by a single instance of the character followed by the count of its occurrences.
- If the compressed string is not strictly shorter than the original, return the original string.
For example:
- If
s = "aaabbbccc"
, then the compressed string isa3b3c3
. - If
s = "abcdef"
, the compression does not shorten the string, so returnabcdef
.
The solution should read the input from stdin
and output the result to stdout
.
inputFormat
The input consists of a single line containing a string s
composed of letters and digits.
Example:
aaabbbccc
outputFormat
Output the compressed string if its length is strictly less than the original. Otherwise, output the original string.
Example:
a3b3c3## sample
aaabbbccc
a3b3c3