#include #include using namespace std; int main() { map left; string s; cin >> s; int ans = 0; int maxi = 0; left[0] = 0; for (int i = 0; i < s.size(); i++) { ans += (s[i] == 'A' ? 1 : -1); if (left.find(ans) == left.end()) { left[ans] = i; } else { maxi = max(maxi, i - left[ans]); } } cout << maxi << endl; return 0; }