forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-complete-substrings.py
33 lines (32 loc) · 1.27 KB
/
count-complete-substrings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Time: O(26 + d * n), d = len(set(word))
# Space: O(26)
# freq table, two pointers, sliding window
class Solution(object):
def countCompleteSubstrings(self, word, k):
"""
:type word: str
:type k: int
:rtype: int
"""
result = valid = 0
cnt = [0]*26
for c in xrange(1, len(set(word))+1):
left = 0
for right in xrange(len(word)):
cnt[ord(word[right])-ord('a')] += 1
curr = cnt[ord(word[right])-ord('a')]
valid += 1 if curr == k else -1 if curr == k+1 else 0
if right-left+1 == c*k+1:
curr = cnt[ord(word[left])-ord('a')]
valid -= 1 if curr == k else -1 if curr == k+1 else 0
cnt[ord(word[left])-ord('a')] -= 1
left += 1
if valid == c:
result += 1
if right+1 == len(word) or abs(ord(word[right+1])-ord(word[right])) > 2:
while left < right+1:
curr = cnt[ord(word[left])-ord('a')]
valid -= 1 if curr == k else -1 if curr == k+1 else 0
cnt[ord(word[left])-ord('a')] -= 1
left += 1
return result