JavaScript required
We’re sorry, but Coda doesn’t work properly without JavaScript enabled.
Skip to content
Gallery
Algoschool
Algocircle
More
Share
Explore
Level 1
Sliding window problems
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/
from collections import defaultdict
class Solution:
def lengthOfLongestSubstringKDistinct(self, s: str, k: int) -> int:
l = 0
m = defaultdict(int)
max_len = 0
for r in range(len(s)):
cur = s[r]
m[cur] += 1
while len(m) > k:
m[s[l]] -= 1
if m[s[l]] == 0:
del m[s[l]]
l += 1
max_len = max(max_len, r - l + 1)
return max_len
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
Ctrl
P
) instead.