i went through Microsoft OA Codility challenge. And came across this question but couldn't find any optimised solution as it said it is not correct. Can anyone suggest a better approach

My Solution :
int solution(string &S) {
int count=1;
int max=1;
int n = S.length();
if(n==0 || n==1){
return 0;
}
int ans[100]={0};
int x=0;
ans[x]=count;
for(int i=1;i<n;i++){
if(S[i]==S[i-1]){
count++;
ans[x]=count;
if(max<count){
max=count;
}
}else{
count=1;
x++;
ans[x]=count;
}
}
int result=0;
int m = sizeof(ans)/sizeof(ans[0]);
for(int i=0;i<m;i++){
if(ans[i]!=0){
int t=(max-ans[i]);
result=result+t;
}
else{
break;
}
}
return result;
}Code passed all the test cases, but the performance score was low. Can anyone suggest optimized space approach?