String compresssion problem only 2 test cases are passing

code link : https://ide.codingblocks.com/s/98207
please tell me what iam doing wrong here ?

Hi @Ajaypandey, everything is correct except for one thing and that is sorting. It is nowhere specified that you have to group all the characters together in the whole string, so just remove that sort function.

Also one tip. Pls see that your code is correct but there is a mistake which is getting neglected and i.e. that at last iteration, i becomes n-1. And in the loop you have compared a[i] with a[i+1]. SO when i=n-1, you check for
a[n-1] & a[n]. So right now it is giving a correct output but ini future it might give some problem so pls correct that thing also. It will take a slight modification in just 2-3 lines.

Hope this helps :slight_smile:

@proRram thanks a lot ! all the test cases have been accepted , but as you mentioned that the last iteration will be between a[n-1] & a[n] ,but without using equal to sign (=)how would i be able to print the last element then … ??

Hi @Ajaypandey, pls change your loop to go from i< n to <n-1. now run your code for 2 testcases:

  1. aaaabbbbcccc
  2. aaaabbbbcccccdddd

pls dry run your code after running both of the testcases and so what goes missing or is left out at last. and try to fill that gap by doing some work just outside the loop.

If you’ll still not get it then I will tell but pls try on your own first. Hope this helps :slightly_smiling_face: