Skip to content

Commit db7bc8d

Browse files
authored
Create count-substrings-that-satisfy-k-constraint-i.py
1 parent e58b0a3 commit db7bc8d

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
# two pointers, sliding window
5+
class Solution(object):
6+
def countKConstraintSubstrings(self, s, k):
7+
"""
8+
:type s: str
9+
:type k: int
10+
:rtype: int
11+
"""
12+
result = cnt = left = 0
13+
for right in xrange(len(s)):
14+
cnt += int(s[right] == '1')
15+
while not (cnt <= k or (right-left+1)-cnt <= k):
16+
cnt -= int(s[left] == '1')
17+
left += 1
18+
result += right-left+1
19+
return result

0 commit comments

Comments
 (0)