#C14662. String Compression Challenge
String Compression Challenge
String Compression Challenge
Given a string S, compress it using counts of repeated characters. For each group of consecutive identical characters, append the character followed by the count of occurrences. For example, the string aabcccccaaa
would be compressed to a2b1c5a3
.
If the compressed string is not strictly shorter than the original string, output the original string instead.
Note: The compressed format is defined by a sequence of character and integer count pairs. For a group of characters c repeated k times, the corresponding segment of the compressed string is given by c followed by k (expressed in base-10). In LaTeX, you can represent the rule as: \(c^k\) (where c is the character and \(k\) is the count).
Example:
- If S =
aabcccccaaa
, compressed string isa2b1c5a3
. - If S =
abcdef
, the compressed string would bea1b1c1d1e1f1
which is not shorter than the original, so outputabcdef
instead.
inputFormat
The input consists of a single line containing the string S to be compressed. The string S may be empty or contain only alphabetic characters.
outputFormat
Output a single line: the compressed string if its length is strictly smaller than the original string, or the original string otherwise.
## sampleaabcccccaaa
a2b1c5a3