using System.Collections.Generic; using System; public class Hello { public static void Main() { var s = Console.ReadLine().Trim(); var sL = s.Length; var a = new int[sL]; for (int i = 0; i < sL; i++) a[i] = s[i] == 'A' ? 1 : -1; var d = new Dictionary(); var ans = 0; d[0] = 0; var b = new int[sL + 1]; for (int i = 1; i <= sL; i++) { b[i] += b[i - 1] + a[i - 1]; if (d.ContainsKey(b[i])) ans = Math.Max(ans, i - d[b[i]]); else d[b[i]] = i; } Console.WriteLine(ans); } }