Problem title
Difficulty
Avg time to solve

Sorted Matrix
Moderate
25 mins
Add First and Second Half
Moderate
35 mins
Odd even level
Easy
20 mins
Left And Right Rotation Of A String
Easy
15 mins
Sorted Linked List to Balanced BST
Moderate
25 mins
Max element in the path
Easy
15 mins
Count Triplets
Easy
15 mins
Given a string, find the next smallest palindrome
Easy
12 mins
Minimum Characters For Palindrome
Hard
20 mins
Minimum steps to reach target by a Knight
Moderate
25 mins
14

# Compress the String

Difficulty: MEDIUM
Contributed By
Avg. time to solve
25 min
Success Rate
60%

Problem Statement

#### Example:

``````If a string has 'x' repeated 5 times, replace this "xxxxx" with "x5".

The string is compressed only when the repeated character count is more than 1.
``````

#### Note :

``````The consecutive count of every character in the input string is less than or equal to 9.
``````
##### Input Format:
``````The first line contains an integer 'T' which denotes the number of test cases or queries to be run.

The first line of each test case contains one string ‘S’ denoting the input string that needs to be compressed.
``````
##### Output Format:
``````For each case, we need to print a string representing the compressed string.

The output of each test case will be printed in a separate line.
``````
##### Note:
``````You do not need to input or print anything, and it has already been taken care of. Just implement the given function.
``````
##### Constraints:
``````1 <= T <= 5
1 <= |S| <= 5000

Where |S| is the size of the string.

Time Limit: 1 sec
``````
##### Sample Input 1:
``````2
aaabbddccc
ggttttffffrreee
``````
##### Sample Output 1:
``````a3b2d2c3
g2t4f4r2e3
``````
##### Explanation of Sample Input 1:
``````Test case 1:
For the first test case of sample output 1, our compressed string is “a3b2d2c3”. It represents that the string contains 3 consecutive ‘a’, 2 consecutive ‘b’, 2 consecutive ‘d’, and 3 consecutive ‘c’.
``````
##### Sample Input 2:
``````1
xyzzz
``````
##### Sample Output 2:
``````xyz3
``````
##### Explanation of Sample Input 2:
``````Test case 1:
For the first test case of sample output 2, our compressed string is ‘xyz3’. As the occurrence of ‘x’ and ‘y’ is 1, hence we do not need to add the count of their repetitions. For the last character ‘z’, we have 3 repetitions.
``````
Console