#C7849. String Compression
String Compression
String Compression
You are given a non-empty string S
consisting of alphabetic characters. Your task is to compress the string by replacing each group of consecutive identical characters with the character followed by the number of occurrences. For example, the string AAABCCDDDD
should be compressed to A3B1C2D4
in LaTeX as: \(A3B1C2D4\).
Important: The compression should only be used if the resulting compressed string is strictly shorter than the original. Otherwise, you should output the original string.
Examples:
- If
S = "aabcccccaaa"
, then the compressed string isa2b1c5a3
. - If
S = "abcdef"
, the compressed versiona1b1c1d1e1f1
is not shorter thanabcdef
, so the output should beabcdef
. - If
S = "AAABCCDDDD"
, then the compressed string isA3B1C2D4
.
inputFormat
The input consists of a single line containing the string S
. The string will only contain uppercase and lowercase English letters.
outputFormat
Output a single line containing the compressed string if its length is strictly less than the original string, otherwise output the original string.
## sampleaabcccccaaa
a2b1c5a3