#K15671. String Compression
String Compression
String Compression
Given a string s
consisting of only uppercase and lowercase English letters, your task is to compress it using counts of consecutive identical characters. For every maximal substring of repeated characters in s
, replace it with the character followed by the number 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.
The compression logic can be formally expressed as follows. Given a string \( s \), construct a compressed string \( s_c \) such that for each group of consecutive characters \( c \) of length \( k \), append \( c\texttt{ + }k \) to \( s_c \). Finally, if \( |s_c| < |s| \), output \( s_c \); otherwise, output \( s \).
inputFormat
The input consists of a single line containing the string s
.
Constraints:
- The string
s
contains only uppercase and lowercase English letters.
outputFormat
Output a single line containing the compressed string according to the algorithm described, or the original string if the compressed version is not strictly shorter.
## sampleaabcccccaaa
a2b1c5a3