File tree 2 files changed +42
-0
lines changed
2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ Beats 90% runtime
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def countOfSubstrings (self , word : str , k : int ) -> int :
3
+ vows = set ('aeiou' )
4
+ conses = set (ascii_letters ) - vows
5
+
6
+ output = 0 # count of the num of substrings
7
+ consonant_cnt = 0 # num of consants in current str
8
+ extra_vowels_on_left = 0 # how many vowels on left side of
9
+ # string that aren't required (i.e.
10
+ # we can choose to include them
11
+ # or exclude them)
12
+
13
+ indices = deque () # Deque to keep track of leftmost required char
14
+ vowels = {} # Tracks right-most occurance of vowel
15
+
16
+ for i , c in enumerate (word ) :
17
+ if c in vows :
18
+ vowels [c ] = i
19
+ else :
20
+ consonant_cnt += 1
21
+ indices .appendleft ((i , c ))
22
+
23
+ # Consonant count must match exactly
24
+ while indices and consonant_cnt > k :
25
+ indx_popped , popped = indices .pop ()
26
+ if popped in conses :
27
+ consonant_cnt -= 1
28
+ elif popped in vows and vowels [popped ] == indx_popped :
29
+ vowels .pop (popped )
30
+ extra_vowels_on_left = 0
31
+
32
+ # Remove unnecessary vowels
33
+ while indices and indices [- 1 ][1 ] in vows and indices [- 1 ][0 ] != vowels [indices [- 1 ][1 ]] :
34
+ extra_vowels_on_left += 1
35
+ indices .pop ()
36
+
37
+ # If extra vowels, we can choose to add x number of vowels to create a new string
38
+ if indices and consonant_cnt == k and len (vowels ) == 5 :
39
+ output += 1 + extra_vowels_on_left
40
+
41
+ return output
You can’t perform that action at this time.
0 commit comments