Keep track of repeating elements


#1

string Solution::solve(string A, int B) {
int n=A.size();
int a[n];
a[n-1]=1;
for(int i=n-2;i>=0;i–){
if(A[i]==A[i+1]){
a[i]=a[i+1]+1;
}
else
a[i]=1;
}
string C="";
int i=0;
while(i<n){
if(a[i]>=B){
while(a[i]!=1)
i++;
}
else
{C=C+A[i];
}
i++;

}
return C;
}


#2

hey
Try this test case :
string = aaaabbcccddddd
B = 3

Your code will give the output = bb
while the correct answer should be abbdd


#3

int n=A.length();
int st[n];st[0]=1;
for(int i=1;i<n;i++){
if(A[i]==A[i-1]){
st[i]=1+st[i-1];
}
else{
st[i]=1;
}
}
string ans="";
for(int i=n-1;i>=0;){
if(st[i]>=B){
i=i-st[i];
}
else{
ans+=A[i];
i–;
}
}
reverse(ans.begin(),ans.end());
return ans;


#4

But, when you see the expected output for the string aaabcd with B = 2, it’s showing the output as aaabcd itself! then why does the string aaaabbcccddddd gives the output aaaabbddddd? Is the problem statement not clear enough to arrive at a common conclusion?


#5

I guess the problem statement is not clear .
This is consistent with few more questions in interviewbit.