4
4
* @return {number }
5
5
*/
6
6
var maxVowels = function ( s , k ) {
7
- let l = 0 , maxCount = 0 , vowelSet = new Set ( [ "a" , "e" , "i" , "o" , "u" ] )
7
+ let l = 0 , maxCount = 0 , vowelSet = new Set ( [ "a" , "e" , "i" , "o" , "u" ] ) , acc = [ ]
8
8
s = s . split ( "" )
9
9
console . log ( s )
10
10
console . log ( vowelSet )
11
11
12
- while ( ( l + k - 1 ) < s . length ) {
13
- let tmpCount = 0 ;
14
- for ( let i = l ; i < ( l + k ) ; i ++ ) {
15
- if ( vowelSet . has ( s [ i ] ) ) tmpCount ++ ;
16
- }
12
+ let tmpSum = 0 ;
13
+ for ( let i = 0 ; i < s . length ; i ++ ) {
14
+ tmpSum += ( vowelSet . has ( s [ i ] ) ? 1 : 0 )
15
+ acc [ i ] = tmpSum ;
16
+ }
17
17
18
- maxCount = Math . max ( maxCount , tmpCount )
18
+ console . log ( acc )
19
19
20
+ while ( ( l + k - 1 ) < s . length ) {
21
+ // console.log("___")
22
+ // console.log(acc[k - 1])
23
+ // console.log((acc[l - 1] ?? 0))
24
+ // console.log(acc[k - 1] - (acc[l - 1] ?? 0))
25
+ maxCount = Math . max ( maxCount , acc [ ( k + l ) - 1 ] - ( acc [ l - 1 ] ?? 0 ) )
20
26
l ++ ;
21
27
}
22
28
@@ -28,8 +34,8 @@ var maxVowels = function (s, k) {
28
34
// ['a', 'b', 'c', 'i', 'i', 'i', 'd', 'e', 'f']
29
35
let x =
30
36
// maxVowels("abciiidef", 3)//3
31
- // maxVowels("aeiou", 2) //2
32
- maxVowels ( "leetcode" , 3 ) //2
37
+ // maxVowels("aeiou", 2) //2
38
+ maxVowels ( "leetcode" , 3 ) //2
33
39
34
- console . log ( "Res" )
35
- console . log ( x )
40
+ console . log ( "Res" )
41
+ console . log ( x )
0 commit comments