File tree 5 files changed +106
-0
lines changed
5 files changed +106
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def numberOfSubstrings (self , s : str ) -> int :
3
+ abc = [- 1 , - 1 , - 1 ]
4
+ output = 0
5
+
6
+ for i , x in enumerate (s ) :
7
+ abc [ord (x ) - ord ('a' )] = i
8
+ if - 1 not in abc :
9
+ output += 1 + min (abc )
10
+
11
+ return output
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def countVowels (self , word : str ) -> int :
3
+ vows = set ('aeiou' )
4
+ output = 0
5
+ for i , c in enumerate (word ) :
6
+ if c in vows :
7
+ output += i * (len (word ) - i - 1 ) + len (word )
8
+ return output
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def countVowels (self , word : str ) -> int :
3
+ vows = set ('aeiou' )
4
+ return sum (i * (len (word ) - i - 1 ) + len (word ) for i , c in enumerate (word ) if c in vows )
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def countVowels (self , word : str ) -> int :
3
+ return sum (i * (len (word ) - i - 1 ) + len (word ) for i , c in enumerate (word ) if c in 'aeiou' )
Original file line number Diff line number Diff line change
1
+ ### Notes and Scratch Work
2
+
3
+ ```
4
+ n = Total length of word
5
+ v = Number of vowels in word
6
+
7
+ aba
8
+ a 1
9
+ b 0
10
+ a 1
11
+ ab 1
12
+ ba 1
13
+ aba 2
14
+
15
+ This is the same as doing "aaaa" instead of "aba" --> becomes 4 choose 2 = 6
16
+
17
+ abc
18
+ a 1
19
+ b 0
20
+ c 0
21
+ ab 1
22
+ bc 0
23
+ abc 1
24
+
25
+ abc -> aaa -> 3 choose 1 = 3
26
+
27
+ abba
28
+ a 1
29
+ b 0
30
+ b 0
31
+ a 1
32
+
33
+ ab 1
34
+ bb 0
35
+ ba 1
36
+
37
+ abb 1
38
+ bba 1
39
+
40
+ abba 2
41
+
42
+ Total: (1)+(1+1)+(1+1+1)+(2) = 8
43
+ abba --> aaaba --> aaaaaa --> 6 choose 2 = 15 WRONG
44
+ abba -> aaba --> aaaaa -> 5 choose 2 = 10 WRONG
45
+ abba -> aaaa --> 4 choose 2 = 6 WRONG
46
+
47
+ contributions:
48
+ a 4
49
+ b 0
50
+ b 0
51
+ a 4
52
+
53
+ abab
54
+ a 1
55
+ b 0
56
+ a 1
57
+ b 0
58
+
59
+ ab 1
60
+ ba 1
61
+ ab 1
62
+
63
+ aba 2
64
+ bab 1
65
+
66
+ abab 2
67
+
68
+ Total = 2+3+3+2 = 10
69
+
70
+ contributions:
71
+ a 4 = 0 * 3 + 4
72
+ b 0
73
+ a 6 = (left * right) + total string length = 2 * 1 + 4
74
+ b 0
75
+
76
+ ```
77
+
78
+ ** Realization** : Each vowel contributes the following to the output:
79
+
80
+ $$ (\text{characters left}) * (\text{characters right}) + \text{word length} $$
You can’t perform that action at this time.
0 commit comments