My issue
What’s wrong with my approach?
I have stored indices of all the c initially.
Then I iterate over the string and count the number of a’s encountered till now.
If the current char is ‘b’ and we have encountered a’s before and there are c’s after , I am checking the min number of elements to be deleted. If the a’s encountered is less, delete a ,else delete c. If we are deleting all the c’s, then there will be no c’s further, so I am breaking out of the loop.
My code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
// your code goes here
int nt;
cin>>nt;
while(nt--){
long long int n;
string s;
cin>>n>>s;
long long int ans=0,counta=0;
queue<int>c;
for(int i=0;i<n;i++){
if(s[i]=='c')c.push(i);
}
for(int i=0;i<n;i++){
if(c.front()==i)c.pop();
if(c.empty())break;
if(s[i]=='a')counta++;
else if(counta and (int)c.size() and s[i]=='b'){
int size=c.size();
if(counta<c.size()){
ans+=counta;
counta=0;
}else{
ans+=c.size();
while(!c.empty())c.pop();
break;
}
}
}
cout<<ans<<endl;
}
return 0;
}
Problem Link: ABC Conjecture 3 Practice Coding Problem - CodeChef