#K70777. String Compression Challenge
String Compression Challenge
String Compression Challenge
You are given a string s
. Your task is to compress the string using run-length encoding. In this encoding, consecutive groups of the same character are represented as the character followed by the number of occurrences. Formally, if a character c appears consecutively k times, it is replaced by ck
(in LaTeX: \( c\,k \)).
After compressing the string, if the length of the compressed string is strictly less than the original string length, output the compressed string; otherwise, output the original string.
Examples:
- Input:
aabcccccaaa
produces output:a2b1c5a3
- Input:
abcd
produces output:abcd
- Input:
abbbbbccaaadddddd
produces output:a1b5c2a3d6
inputFormat
The input consists of a single line containing a non-empty string s
made up of lowercase letters.
You can assume the input string has a length that fits into memory.
outputFormat
Output the compressed string if the length of the compressed string is strictly smaller than the original string length. Otherwise, print the original string.
## sampleaabcccccaaa
a2b1c5a3