Skip to content

Commit 78665ed

Browse files
authored
Create number-of-equal-count-substrings.py
1 parent 2f9bfd2 commit 78665ed

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(26 * n) = O(n)
2+
# Space: O(26) = O(1)
3+
4+
class Solution(object):
5+
def equalCountSubstrings(self, s, count):
6+
"""
7+
:type s: str
8+
:type count: int
9+
:rtype: int
10+
"""
11+
result = 0
12+
for l in xrange(1, min(len(set(s)), len(s)//count)+1):
13+
cnt, equal_cnt = collections.Counter(), 0
14+
for i, c in enumerate(s):
15+
cnt[c] += 1
16+
equal_cnt += (cnt[c] == count)
17+
if count*l <= i:
18+
equal_cnt -= (cnt[s[i-count*l]] == count)
19+
cnt[s[i-count*l]] -= 1
20+
result += (equal_cnt == l)
21+
return result

0 commit comments

Comments
 (0)