-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnumber-of-substrings-containing-all-three-characters.py
53 lines (46 loc) · 1.71 KB
/
number-of-substrings-containing-all-three-characters.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
# https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/
class Solution:
def numberOfSubstrings(self, s: str) -> int:
# get the length
n = len(s)
# use a map to store the eleems
mpp = {}
# start point of sliding window
start = 0
# store the count of substring as result
total_substring = 0
# use the sliding window concept
# as we need to find the substrings
for end in range(n):
# get the character from the string
ch = s[end]
# add the letter into the mpp
mpp[ch] = mpp.get(ch, 0) + 1
# while we have all 3 chars, try to minimize window from start
while len(mpp) == 3:
total_substring += (n - end)
# remove start char from window
mpp[s[start]] -= 1
if mpp[s[start]] == 0:
del mpp[s[start]]
start += 1
# return the result
return total_substring
def numberOfSubstringsNative(self, s: str) -> int:
# get the length
n = len(s)
# results
results = 0
# iterate on the arr
for i in range(n):
# use dict instead of defaultdict
mpp = {}
for j in range(i, n):
# add character to map
mpp[s[j]] = mpp.get(s[j], 0) + 1
# check if all 3 chars present
if len(mpp) == 3:
# add remaining length since all substrings after this will contain abc
results += n - j
break
return results