题目:原题链接(中等)
标签:滑动窗口、哈希表、字符串
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
Ans 1 (Python) | 64ms (59.14%) | ||
Ans 2 (Python) | |||
Ans 3 (Python) |
解法一:
class Solution:
def numKLenSubstrNoRepeats(self, s: str, k: int) -> int:
count = [0] * 26
if len(s) < k:
return 0
for i in range(k):
a = ord(s[i]) - 97
count[a] += 1
ans = 1 if max(count) <= 1 else 0
for i in range(k, len(s)):
a1 = ord(s[i]) - 97
count[a1] += 1
a2 = ord(s[i - k]) - 97
count[a2] -= 1
if max(count) <= 1:
ans += 1
return