코드
import java.lang.*
import java.util.*
var n = 0
var str = ""
var result = 0
val map = HashMap<Char, Int>()
fun main() {
input()
func()
println(result)
}
fun input() {
val sc = Scanner(System.`in`)
n = sc.nextInt()
str = sc.next()
sc.close()
}
fun func() {
var left = 0
var right = 0
while (true) {
val length = map.keys.size
if (length <= n) {
if (result < (right - left))
result = right - left
if (right == str.length) return
val count = map[str[right]] ?: 0
map[str[right]] = count + 1
++right
} else {
val count = map[str[left]]!!
if (count == 1) map.remove(str[left])
else map[str[left]] = count - 1
++left
}
}
}