#C5663. String Compression
String Compression
String Compression
Given a string s
, you are to compress it by replacing consecutive repeated characters with the character followed by the count of repetitions. For example, the string "aabcccccaaa" would become "a2b1c5a3". However, if the compressed string is not strictly shorter than the original, you must return the original string.
The compression algorithm works as follows:
Let \(S\) be the original string and \(C\) be the compressed string. For each sequence of repeated characters in \(S\), append the character and the count (in decimal) to \(C\). Return \(C\) if \(|C| < |S|\); otherwise, return \(S\).
Note: The input string may be empty. In that case, simply output an empty string.
inputFormat
The input consists of a single line containing the string s
that you need to compress.
Note: The string can include any printable characters and may be empty.
outputFormat
Output a single line containing the compressed string if its length is strictly less than that of the input string; otherwise, output the original string.
## sampleaabcccccaaa
a2b1c5a3