#K38912. String Compression
String Compression
String Compression
Given a string s
, compress it by replacing each group of consecutive identical characters with the character followed by the number of occurrences if the count is greater than 1. Otherwise, if the character occurs only once, output the character as is. For example, if s = "aaabbcaaa"
, then the compressed string is a3b2ca3
.
Your task is to implement the compression algorithm as described. The order of characters must be maintained and only groups of consecutive characters are compressed.
The solution should read from stdin
and output the result to stdout
.
inputFormat
A single line of input containing the string s to be compressed. The string consists of lowercase English letters and may be empty.
outputFormat
Output the compressed string as defined in the problem statement.## sample
aaabbcaaa
a3b2ca3