Compress the String

Moderate
0/80
Average time to solve is 25m
profile
Contributed by
33 upvotes
Asked in companies
InfosysMathworksHarman International

Problem statement

Ninja has been given a program to do basic string compression. For a character that is consecutively repeated more than once, he needs to replace the 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 :

The consecutive count of every character in the input string is less than or equal to 9.
Detailed explanation ( Input/output format, Notes, Images )
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.
Hint

Keep a count of each consecutive repeating character traveling the string.

Approaches (1)
Greedy Approach

Here, we can simply traverse the string and run two loops where the outer loop will hold the unique characters and the inner loop will count the consecutive repetitions of that character. Once, we encounter a new character, our inner loop breaks and now our outer loop will start again from the new character. In this process, while getting the repetitions of each character, we can append the character and its count of repetitions depending on the condition that the repetition should be greater than 1.

 

Algorithm:

 

  • Declare an integer variable ‘n’ to store the length of the input string
  • Declare a string variable ‘compressString’ to hold the final compressed string.
  • Declare an integer variable ‘localCount’ to keep the count of repetitions of the current character of the string.
  • Declare a character type variable ‘currentCharacter’ to store the current character of the string.
  • Declare two integer variables ‘i’ and ‘j’ as two loop counters.
  • Run a loop from ‘i’ = ‘0’ to ‘n’.
    • Set ‘currentCharacter’ to ‘s[i]’ denoting the current character
    • Set ‘’localCount’ to ‘0’
    • Run a loop from ‘j’ = ‘i’ to ‘n’
      • If our ‘currentCharacter’ is equal to ‘s[j]’
        • Increase ‘localCount’ by 1
      • Otherwise, we have encountered a different character and we need to break the loop
    • If our ‘localCount’ is equal to 1 denoting a single occurrence of the ‘currentCharacter’
      • Append only that character to the ‘compressString’
    • Otherwise, if ‘localCount’ is greater than 1
      • Append the character and ‘localCount’ to ‘compressString’.
    • Set ‘i’ equal to ‘j - 1’ because ‘j’ has already encountered a different character. Hence, ‘i’ needs to be set one step back so that on the next increment of ‘i’, it will start from that different character.
  • Return ‘compressString’.
Time Complexity

O(N) where N is the length of the input string.

 

Though we are using two loops to iterate through the string, our outer loop is only iterating through the different characters present in the string while the inner loop is iterating to count the repetition of the current character in the outer loop. Hence, each character of the string is only traversed once, so our time complexity is O(N).

Space Complexity

O(1)

 

We only need constant space. So it’s O(1).

Code Solution
(100% EXP penalty)
Compress the String
Full screen
Console