Close
Topic list
Compress the String
MEDIUM
32 mins
Strings
Topics (Covered in this problem)
Problem solved
Skill meter
Strings
-
Other topics
Problem solved
Skill meter
Matrices (2D Arrays)
-
Sorting
-
Binary Search
-
-
Stacks & Queues
-
Trees
-
Graph
-
Dynamic Programming
-
Greedy
-
Tries
-
Arrays
-
Binary Search Trees
-
Heap
-
Bit Manipulation
-

# Compress the String

Contributed by
Deep Mavani
Medium
Avg time to solve 32 mins
Success Rate 60 %
Share

## Problem Statement

#### Write a program to do basic string compression. For a character which is consecutively repeated more than once, replace consecutive duplicate occurrences with the count of repetitions.

##### 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:
``````Consecutive count of every character in the input string is less than or equal to 9.
``````
Detailed explanation ( Input/output format, Notes, Constraints, Images )
##### Sample Input 1:
``````aaabbccdsa
``````
##### Sample Output 1:
``````a3b2c2dsa
``````
##### Explanation for Sample Output 1:
``````In the given string 'a' is repeated 3 times, 'b' is repeated 2 times, 'c' is repeated 2 times and 'd', 's' and 'a' and occuring 1 time hence no compression for last 3 characters.
``````
##### Sample Input 2:
``````aaabbcddeeeee
``````
##### Sample Output 2:
``````a3b2cd2e5
``````
##### Explanation for Sample Output 2:
``````In the given string 'a' is repeated 3 times, 'b' is repeated 2 times, 'c' is occuring single time, 'd' is repeating 2 times and 'e' is repeating 5times.
``````
Console