what is wrong with my code,can you modify it
int main(){
string s;
cin>>s;
string str;
str=s[0];
int count=1;
for(int i=1;i<s.length();i++){
char curr=s[i];
char pre=s[i-1];
if(curr==pre){
count++;
}
else{
if(count>1){
str=str+count;
count=1;
}
str=str+curr;
}
}
if(count>1){
str=str+count;
count=1;
}
cout<<str;
}
String compression
why did you use the second if else condition in the code
hi @dips123deepali_c25f140838182212
i have used that if else to check the corner cases… it may be possible that the last character occurs only single time or it is repeating… so just to handle these cases i have used that… try to dry run on 2-3 test cases, u will get better insight…
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.