python code:
t=int(input())
while t:
t=t-1; x=input(); stack=[]
for a in x:
stack.append(a) if len(stack)==0 else stack.pop()if a=='B' else stack.append(a)
print(len(stack))
C++ code :
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
string x;
cin >> t;
while(t--)
{
cin >> x;
stack<int> st;
for(int i=0;i<x.size();i++)
{
if(st.size()==0)
st.push(x[i]);
else if(x[i]=='B')
st.pop();
else
st.push(x[i]);
}
cout << st.size()<< endl;
}
}
No comments:
Post a Comment