File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @param {number } k
4
+ * @return {number }
5
+ */
6
+ const takeCharacters = function ( s , k ) {
7
+ const n = s . length
8
+ const cnt = Array ( 3 ) . fill ( 0 )
9
+ const a = 'a' . charCodeAt ( 0 )
10
+ for ( const ch of s ) {
11
+ cnt [ ch . charCodeAt ( 0 ) - a ] ++
12
+ }
13
+ const target = Array ( 3 ) . fill ( 0 )
14
+ for ( let i = 0 ; i < 3 ; i ++ ) {
15
+ target [ i ] = cnt [ i ] - k
16
+ }
17
+ for ( let e of target ) {
18
+ if ( e < 0 ) return - 1
19
+ }
20
+ const arr = Array ( 3 ) . fill ( 0 )
21
+ let res = 0
22
+ let i = 0
23
+ for ( let j = 0 ; j < n ; j ++ ) {
24
+ const idx = s [ j ] . charCodeAt ( 0 ) - a
25
+ arr [ idx ] ++
26
+ while ( ! valid ( ) ) {
27
+ const ii = s [ i ] . charCodeAt ( 0 ) - a
28
+ arr [ ii ] --
29
+ i ++
30
+ }
31
+ res = Math . max ( res , j - i + 1 )
32
+ }
33
+
34
+ return n - res
35
+
36
+ function valid ( ) {
37
+ return arr . every ( ( e , i ) => e <= target [ i ] )
38
+ }
39
+ } ;
40
+
41
+ // another
42
+
1
43
/**
2
44
* @param {string } s
3
45
* @param {number } k
You can’t perform that action at this time.
0 commit comments