fun main(args:Array) { val inputs = readLine()!!.toList().map { when { it == 'A' -> 1 else -> -1 } } // scanner var s = 0 val tri = mutableListOf(0) val m = mutableMapOf( 0 to 1 ) inputs.map { s += it if( m.get(s) == null ) { m[s] = 1 } else { m[s] = m[s]!! + 1 } tri.add(s) } val pats = m.toList().filter { val (k,v) = it v >= 2 }.map { val (k,v) = it k } 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) }