Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

String compression using the counts of repeated characters.

Input : A string // abbcccdddd
Output : A string // ab2c3d4

Logic :

  • Iterate over the string and compare the previous character with the current character.
  • If both characters are the same increment the count by 1.
  • Else update the compressed string based on the value of count.
    • If count is 1 then do not concat the count to compressed string.
    • Else concat the count to compressed string and reset the count to 1.
  • Apply the same logic once finished iterating over the string.

Solution :

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *