String compression

I wrote this code it works for only 2/4 test cases I wanted to unlock the other test cases but doesn’t allow me could you please tell me the problem in the code.

//#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
if(s.length()>1)
for(int i=0;i<s.length();i++)
{ int count=1;
for(int j=i+1;j<s.length();j++)
{
if(s[i]==s[j])
{
count++;
}
}
cout<<s[i]<<count;
i=i+count-1;
}
if(s.length()==1)
cout<<s<<“1”;
}

hi @gurkirat2600
image
just add this else statement… ur code will work fine and pass all test cases…

corrected code --> https://ide.codingblocks.com/s/643156

hi @gurkirat2600
is it clear now or still there is any doubt regarding it???

It is clear thank you

1 Like

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.

I cannot open the link it says error 404

Try refreshing it… its opening…
here’s the code -->

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    cin>>s;
    for(int i=0;i<s.length();i++)
    { int count=1;
        for(int j=i+1;j<s.length();j++)
        {
            if(s[i]==s[j])
            {
                count++;
            }
            else{
                break;
            }
        }
        cout<<s[i]<<count;
        i=i+count-1;
    }
}