#K36067. String Compression Challenge
String Compression Challenge
String Compression Challenge
Given a string S, compress it by replacing each consecutive group of the same character with the character followed by the count of its repetitions. For instance, the string "aaabbc" should be compressed to "a3b2c1". The program must read an integer T followed by T lines, each representing a test string. For each test string, output its compressed form on a new line.
The compression is performed as follows:
( S = s_1 s_2 \ldots s_n )
For every group of consecutive equal characters ( s_i ), output the character and the number of times it repeats consecutively.
For example:
- Input string: ( "aaabbc" ) produces output: ( "a3b2c1" ).
- Input string: ( "hello" ) produces output: ( "h1e1l2o1" ).
Implement the solution using standard input and output.
inputFormat
The first line contains an integer ( T ) representing the number of test cases. Each of the following ( T ) lines contains a non-empty string ( S ) that needs to be compressed. It is guaranteed that ( S ) only contains printable characters.
outputFormat
For each test case, output a single line representing the compressed version of the string ( S ).## sample
3
aaabbc
hello
aabbcc
a3b2c1
h1e1l2o1
a2b2c2
</p>