#include #include #include #include #include #include #include #include #include #include using namespace std; #define int long long int MOD = 1000000007; signed main() { cin.tie(0); ios::sync_with_stdio(false); string S; cin >> S; int N = S.size(); vector A(N + 1, 0); int res = 0; for (int i = 0; i < N; i++) { if (S[i] == 'A') { A[i + 1] = A[i] + 1; } else { A[i + 1] = A[i] - 1; } } map mpr; map mpl; for (int i = 0; i < N + 1; i++) { mpr[A[i]] = i; } for (int i = N ; i >= 0; i--) { mpl[A[i]] = i; } for (auto x : mpl) { if (mpr.count(x.first)) { res = max(res, mpr[x.first] - x.second); } } cout << res << endl; }