fun main(args:Array) { val inputs = readLine()!!.toList().map { when { it == 'A' -> 1 else -> -1 } } // scanner var s = 0 val tri = mutableListOf(0) inputs.map { s += it tri.add(s) } val pats = tri.toSet() val ans = mutableListOf() pats.map { pat -> var start = -1 for( _start in (0..tri.size-1) ) { if( tri[_start] == pat ) { start = _start break } } var end = -1 for( _end in (tri.size-1 downTo 0) ) { if( tri[_end] == pat ) { end = _end break } } ans.add( end - start ) } ans.max()?.let(::println) ?: println(0) }