-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1358.Number_of_Substrings_Containing_All_Three_Characters.py
More file actions
69 lines (48 loc) · 1.9 KB
/
1358.Number_of_Substrings_Containing_All_Three_Characters.py
File metadata and controls
69 lines (48 loc) · 1.9 KB
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
"""
Given a string s consisting only of characters a, b and c.
Return the number of substrings containing at least one occurrence of all these characters a, b and c.
Example 1:
Input: s = "abcabc"
Output: 10
Explanation: The substrings containing at least one occurrence of the characters a, b and c are "abc", "abca", "abcab", "abcabc", "bca", "bcab", "bcabc", "cab", "cabc" and "abc" (again).
Example 2:
Input: s = "aaacb"
Output: 3
Explanation: The substrings containing at least one occurrence of the characters a, b and c are "aaacb", "aacb" and "acb".
Example 3:
Input: s = "abc"
Output: 1
Constraints:
3 <= s.length <= 5 x 10^4
s only consists of a, b or c characters.
"""
class Solution:
def numberOfSubstrings(self, s: str) -> int:
ch_to_freq = collections.defaultdict(int)
res = 0
i = 0
for j, ch in enumerate(s):
ch_to_freq[ch] += 1
#满足条件
while i <= j and len(ch_to_freq) >= 3:
res += len(s) - j #len(s) - j 表示以 i 开头的valid_substring的个数, j为满足条件的字串的最后一个下标
ch_to_freq[s[i]] -= 1
if ch_to_freq[s[i]] == 0:
del ch_to_freq[s[i]]
i += 1
return res
class Solution:
def numberOfSubstrings(self, s: str) -> int:
ch_to_cnt = defaultdict(int)
j = 0
res = 0
for i in range(len(s)):
while j < len(s) and len(ch_to_cnt) < 3:
ch_to_cnt[s[j]] += 1
j += 1
if len(ch_to_cnt) >= 3:
res += len(s) - j + 1 #len(s) - j + 1 表示以 i 开头的valid_substring的个数
ch_to_cnt[s[i]] -= 1
if ch_to_cnt[s[i]] == 0:
del ch_to_cnt[s[i]]
return res