I am trying to erase particular characters using the erase() function from a string but its not working.
The question says you have to remove a substring which is either "AB" or "BB". When the substring gets deleted from the string the remaining parts of the string get concatenated and the process continues...
Here is the code:
#include <bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int long long int
using namespace std;
int32_t main()
{
ios;
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int i;
for(i=0;i<s.length();i++)
{
if(s[i]=='A'&&s[i+1]=='B')
{
s.erase(i,i+1);
cout<<s<<"\n";
i=-1; // I want to start from begining therefore initializing i=-1 after i++ it becomes i=0;
}
else if(s[i]=='B'&&s[i+1]=='B')
{
s.erase(i,i+1);
cout<<s<<"\n";
i=-1;
}
}
//cout<<s.length()<<"\n";
}
return 0;
}
Input:
1
AABBBABBBB
The output is :
ABBABBBB
BBABBBB
BABBBB
BBBB
BBB
BB
B
But the output should be:
ABBABBBB
BABBBB
BBBB
BB
Am I doing something wrong?