String Compression

My default test is not passing that is aaabbccds is printing
a3b2c2sd rather than a3b2c2ds but rest is printing correctly example aaabbss is printing a3b2s2 please help and tell me where am i going wrong in my algorithm

coding Block IDE : https://ide.codingblocks.com/s/591760

Hey @rajputsiddharth18,
You are facing this issue because the map stores the element in random order (according to the hash function) and not in the order in which it is passed.
Your algorithm will also fail the cases where the input string is like aabbaa.

In order to solve this simply create the answer while traversing on the string and keep count of the consecutive elements.

Hope it helps. :slight_smile:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.