-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path1513.number-of-substrings-with-only-1-s.py
60 lines (53 loc) · 1.63 KB
/
1513.number-of-substrings-with-only-1-s.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
#
# @lc app=leetcode id=1513 lang=python3
#
# [1513] Number of Substrings With Only 1s
#
# @lc code=start
# TAGS: String, Math
import collections
class Solution:
# 128 ms, 31.21 %. Time and Space: O(N)
# not optimal because we have to calculate the combination table
def numSub(self, s: str) -> int:
# Find the frequencies of each length of substring.
freq = collections.defaultdict(int)
cnt = 0
s += '0'
for c1, c2 in zip(s, s[1:]):
if c1 == '1':
cnt += 1
if c1 != c2:
freq[cnt] += 1
elif c1 == '0':
cnt = 0
# Generate combination table
# 1: 1 = 1
# 2: 1 + 2 = 3
# 3: 1 + 2 + 3 = 6
# 4: 1 + 2 + 3 + 4 = 10
# 5: 1 + 2 + 3 + 4 + 5 = 15
combination = [0]
for i in range(1, len(s) + 1):
combination.append(combination[-1] + i)
total = 0
for k, v in freq.items():
total += combination[k] * v
return total % (10**9 + 7)
# 84 ms, 65.37 %. Time and Space: O(N). Similar to above but more optimized.
def numSub(self, s: str) -> int:
freq = collections.defaultdict(int)
cnt = 0
s += '0'
for c1, c2 in zip(s, s[1:]):
if c1 == '1':
cnt += 1
if c1 != c2:
freq[cnt] += 1
elif c1 == '0':
cnt = 0
total = 0
for k, v in freq.items():
total += (k + 1) * k // 2 * v
return total % (10**9 + 7)
# @lc code=end