File tree 1 file changed +11
-10
lines changed
scripts/algorithms/C/Count Number of Texts
1 file changed +11
-10
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 134 ms (Top 52.17%) | Memory: 48.8 MB (Top 91.30%)
1
2
var countTexts = function ( pressedKeys ) {
2
3
const MOD = 1e9 + 7 ;
3
4
const n = pressedKeys . length ;
4
5
const dp = new Array ( n + 1 ) . fill ( 0 ) ;
5
-
6
+
6
7
dp [ 0 ] = 1 ;
7
-
8
+
8
9
let lastChar = "" ;
9
10
let repeatCount = 0 ;
10
-
11
+
11
12
for ( let i = 1 ; i <= n ; ++ i ) {
12
13
const currChar = pressedKeys [ i - 1 ] ;
13
-
14
+
14
15
if ( currChar != lastChar ) repeatCount = 0 ;
15
-
16
+
16
17
lastChar = currChar ;
17
18
repeatCount += 1 ;
18
-
19
+
19
20
dp [ i ] = ( dp [ i ] + dp [ i - 1 ] ) % MOD ;
20
-
21
+
21
22
if ( i >= 2 && repeatCount >= 2 ) dp [ i ] = ( dp [ i ] + dp [ i - 2 ] ) % MOD ;
22
23
if ( i >= 3 && repeatCount >= 3 ) dp [ i ] = ( dp [ i ] + dp [ i - 3 ] ) % MOD ;
23
- if ( ( currChar == "7" || currChar == "9" ) && i >= 4 && repeatCount >= 4 ) dp [ i ] = ( dp [ i ] + dp [ i - 4 ] ) % MOD ;
24
+ if ( ( currChar == "7" || currChar == "9" ) && i >= 4 && repeatCount >= 4 ) dp [ i ] = ( dp [ i ] + dp [ i - 4 ] ) % MOD ;
24
25
}
25
-
26
+
26
27
return dp [ n ] ;
27
- } ;
28
+ } ;
You can’t perform that action at this time.
0 commit comments