#K42347. String Run-Length Encoding
String Run-Length Encoding
String Run-Length Encoding
You are given a string s consisting of printable characters. Your task is to encode the string using a simple run-length encoding scheme.
In this scheme, for each contiguous block of identical characters, output the character itself, and if the block contains more than one occurrence, append the number of occurrences. Formally, for a given string s, the encoding is defined as follows:
Let \(s = c_1c_2\cdots c_n\). For each maximal group of consecutive identical characters \(c, c, \dots, c\) (with count \(k\)), output c followed by \(k\) if \(k > 1\); otherwise, output only c.
For example:
aaabbbccca
is encoded asa3b3c3a
.abc
is encoded asabc
.aabbccdd
is encoded asa2b2c2d2
.xxxxxxxxxx
is encoded asx10
.
You need to implement the encoding logic such that the program reads input from stdin
and writes the result to stdout
.
inputFormat
The input consists of a single line containing the string s to be encoded. The string will only contain printable characters and will not be empty.
Input Format: A single line read from stdin
.
outputFormat
Output the encoded string to stdout
.
Output Format: A single line containing the encoded string.
## sampleaaabbbccca
a3b3c3a