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[a[0]] = 0; for (int i = 1; i < sL; i++) { a[i] += a[i - 1]; if (d.ContainsKey(a[i])) ans = Math.Max(ans, i - d[a[i]]); else d[a[i]] = i; } Console.WriteLine(ans); } }