-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathcount-the-number-of-substrings-with-dominant-ones.py
73 lines (68 loc) · 2.43 KB
/
count-the-number-of-substrings-with-dominant-ones.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Time: O(n * sqrt(n)) = O(n^(3/2))
# Space: O(n)
# two pointers, sliding window
class Solution(object):
def numberOfSubstrings(self, s):
"""
:type s: str
:rtype: int
"""
result = 0
idxs = [-1]+[i for i, x in enumerate(s) if x == '0']+[len(s)]
curr = 1
for i in xrange(len(s)):
if idxs[curr] == i:
curr += 1
for c in xrange(min(int((-1+(1+4*(i+1))**0.5)/2)+1, curr)): # since c^2 <= (i+1)-c, thus c <= (-1+(1+4*(i+1))**0.5)/2
if c**2 <= (i-idxs[(curr-c)-1])-c:
result += min(min(idxs[curr-c], i)-idxs[(curr-c)-1], ((i-idxs[(curr-c)-1])-c)-c**2+1)
return result
# Time: O(n * sqrt(n)) = O(n^(3/2))
# Space: O(n)
# two pointers, sliding window
class Solution2(object):
def numberOfSubstrings(self, s):
"""
:type s: str
:rtype: int
"""
result = 0
idxs = [-1]+[i for i, x in enumerate(s) if x == '0']+[len(s)]
for c in xrange(int((-1+(1+4*len(s))**0.5)/2)+1): # since c^2 <= n-c, thus c <= (-1+(1+4*n)**0.5)/2
left = right = 1
for i in xrange(len(s)):
if idxs[right] == i:
right += 1
if right-left == c+1:
left += 1
if not (right-left == c and ((i-idxs[left-1])-c) >= c**2):
continue
result += min(min(idxs[left], i)-idxs[left-1], ((i-idxs[left-1])-c)-c**2+1)
return result
# Time: O(n * sqrt(n)) = O(n^(3/2))
# Space: O(1)
# two pointers, sliding window
class Solution_TLE(object):
def numberOfSubstrings(self, s):
"""
:type s: str
:rtype: int
"""
result = 0
for c in xrange(int((-1+(1+4*len(s))**0.5)/2)+1): # since c^2 <= n-c, thus c <= (-1+(1+4*n)**0.5)/2
cnt = [0]*2
left = curr = 0
for right in xrange(len(s)):
cnt[s[right] == '1'] += 1
while cnt[0] == c+1:
cnt[s[left] == '1'] -= 1
left += 1
if not (cnt[0] == c and cnt[1] >= c**2):
continue
for curr in xrange(max(curr, left), right):
if s[curr] == '0':
break
else:
curr = right
result += min(curr-left+1, cnt[1]-c**2+1)
return result