let S = stdin.ReadLine().ToCharArray() let ans = let mem = System.Collections.Generic.Dictionary() mem.Add(0,-1) S |> Array.indexed |> Array.fold(fun (counter,maxLen) (idx,c) -> let counter = match c with |'A' -> counter + 1 | _ -> counter - 1 match mem.TryGetValue counter with | true, lIdx -> (counter, max maxLen (idx - lIdx)) | _ -> mem.Add(counter,idx); (counter, maxLen)) (0,0) |> fun (counter,maxLen) -> maxLen ans |> printfn "%i"