String compression

A test case fails which test case I miss

#include
#include
using namespace std;
int main() {
char a[1000];
cin.getline(a,1000);
int freq[26]={0};
for(int i=0;a[i]!=’\0’;i++){
freq[a[i]-β€˜a’]++;

}
for(int i=0;i<26;i++){
	// ch
	char x=i+'a';
	 if (freq[i]>0){
		if(freq[i]>1){
			cout<<x<<freq[i];
		}
		else{
			cout<<x;
		}
    
	 }
	
}
return 0;

}

1 Like

hi @kumarakash121005
refer this–>

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    cin>>s;

    if(s.length()==1){
        cout<<s<<"1";
        return 0; 
    } 

    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;
    }
    return 0;
}

What is wrong in my code

hi @kumarakash121005
what u are doing is first calculating the frequencies of all characters and then simply printing them…
suppose input is aabaa
ur o/p --> a4b
expected o/p -->a2b1a2

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.

what is the reason behind i=i+count-1