#include
#include
using namespace std;
char* str_compression(string s)
{
int n=s.length(),count=1,k=0;
char* ptr=new char[2n];
for(int i=0;i<n;i++)
{
if(s[i]==s[i+1])
count++;
else
{
ptr[k++]=s[i];
ptr[k++]=count+‘0’;
count=1;
}
}
ptr[k]=’\0’;
return ptr;
}
int main()
{
string s;
cin>>s;
char ans=str_compression(s);
cout<<ans;
delete ans;
return 0;
}
Failing for one test case?
Hello @namangarg31,
You have missed an important case:
As per the given constraint:
1 < = length of String < = 1000
For example like:
aaaaaaaaaaaaaaaa
where a character is repeating more than 9 times, your code will fail.
Expected Output:
a16
Your Output:
a@
Correct it.
Hope, this would help.
Give a like if you are satisfied.
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.