题目:
题解:
func longestSubstring(s string, k int) (ans int) {
for t := 1; t <= 26; t++ {
cnt := [26]int{}
total := 0
lessK := 0
l := 0
for r, ch := range s {
ch -= 'a'
if cnt[ch] == 0 {
total++
lessK++
}
cnt[ch]++
if cnt[ch] == k {
lessK--
}
for total > t {
ch := s[l] - 'a'
if cnt[ch] == k {
lessK++
}
cnt[ch]--
if cnt[ch] == 0 {
total--
lessK--
}
l++
}
if lessK == 0 {
ans = max(ans, r-l+1)
}
}
}
return ans
}
func max(a, b int) int {
if a > b {
return a
}
return b
}