using System; using System.Collections.Generic; using System.ComponentModel; namespace ConsoleApp2 { class Program { static void Main(string[] args) { String s = Console.ReadLine(); Int32 n = s.Length; var mp = new Dictionary(); Int32 p = 0; mp[0] = -1; Int32 ans = 0; for (Int32 i = 0; i < n; i++) { if (s[i] == 'A') p++; else p--; if (mp.ContainsKey(p)) ans = Math.Max(ans, i - mp[p]); else mp[p] = i; } Console.WriteLine(ans); } } }