#include #include using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); static char S[200001]; cin >> S; static uint32_t first_index[400001], ans = 0, count_A = 0, count_B = 0, i; for (i = 0; i <= 400001; ++i) first_index[i] = UINT32_MAX; first_index[0] = 0; for (i = 0; ; ++i) { switch (S[i]) { case 'A': ++count_A; break; case 'B': ++count_B; break; case '\0': cout << ans << '\n'; return 0; } if (first_index[200000 + count_A - count_B] == UINT32_MAX) first_index[200000 + count_A - count_B] = i + 1; else if (ans < i + 1 - first_index[200000 + count_A - count_B]) ans = i + 1 - first_index[200000 + count_A - count_B]; } return -1; }