#K89327. String Compression
String Compression
String Compression
You are given a string s
. Your task is to compress the string by replacing each sequence of consecutive identical characters with the character followed by the count of that sequence, but only if the count is greater than 1. If a character appears only once consecutively, it should remain unchanged.
For example, if s = "aaabbc"
then the output should be a3b2c
, and if s = "abc"
then the output should be abc
.
Your solution should read the input from standard input (stdin
) and print the result to standard output (stdout
).
The compression operation can be formally defined as follows: \[ \text{compress_string}(s) = \begin{cases} \text{empty string,} & \text{if } s \text{ is empty} \\ c \quad\text{if a character } c \text{ appears only once consecutively} \\ c\,k \quad\text{if a character } c \text{ appears } k \ (k > 1) \text{ times consecutively} \end{cases} \]
inputFormat
The input consists of a single line containing the string s
. The string can include letters, digits, and symbols.
outputFormat
Output a single line containing the compressed version of the string.
## sampleaaabbc
a3b2c