@@ -60,13 +60,96 @@ Since we need to return the answer modulo 10<sup>9</sup> + 7, we return 20828761
60
60
### ** Python3**
61
61
62
62
``` python
63
-
63
+ mod = 10 ** 9 + 7
64
+ f = [1 , 1 , 2 , 4 ]
65
+ g = [1 , 1 , 2 , 4 ]
66
+ for _ in range (100000 ):
67
+ f.append((f[- 1 ] + f[- 2 ] + f[- 3 ]) % mod)
68
+ g.append((g[- 1 ] + g[- 2 ] + g[- 3 ] + g[- 4 ]) % mod)
69
+
70
+
71
+ class Solution :
72
+ def countTexts (self , pressedKeys : str ) -> int :
73
+ ans = 1
74
+ for ch, s in groupby(pressedKeys):
75
+ m = len (list (s))
76
+ ans = ans * (g[m] if ch in " 79" else f[m]) % mod
77
+ return ans
64
78
```
65
79
66
80
### ** Java**
67
81
68
82
``` java
83
+ class Solution {
84
+ private static final int N = 100010 ;
85
+ private static final int MOD = (int ) 1e9 + 7 ;
86
+ private static long [] f = new long [N ];
87
+ private static long [] g = new long [N ];
88
+ static {
89
+ f[0 ] = 1 ;
90
+ f[1 ] = 1 ;
91
+ f[2 ] = 2 ;
92
+ f[3 ] = 4 ;
93
+ g[0 ] = 1 ;
94
+ g[1 ] = 1 ;
95
+ g[2 ] = 2 ;
96
+ g[3 ] = 4 ;
97
+ for (int i = 4 ; i < N ; ++ i) {
98
+ f[i] = (f[i - 1 ] + f[i - 2 ] + f[i - 3 ]) % MOD ;
99
+ g[i] = (g[i - 1 ] + g[i - 2 ] + g[i - 3 ] + g[i - 4 ]) % MOD ;
100
+ }
101
+ }
102
+
103
+ public int countTexts (String pressedKeys ) {
104
+ long ans = 1 ;
105
+ for (int i = 0 , n = pressedKeys. length(); i < n; ++ i) {
106
+ int j = i;
107
+ char c = pressedKeys. charAt(i);
108
+ for (; j + 1 < n && pressedKeys. charAt(j + 1 ) == c; ++ j);
109
+ int cnt = j - i + 1 ;
110
+ ans = c == ' 7' || c == ' 9' ? ans * g[cnt] : ans * f[cnt];
111
+ ans %= MOD ;
112
+ i = j;
113
+ }
114
+ return (int ) ans;
115
+ }
116
+ }
117
+ ```
69
118
119
+ ### ** Go**
120
+
121
+ ``` go
122
+ const mod int = 1e9 + 7
123
+ const n int = 1e5 + 10
124
+
125
+ var f = [n]int {1 , 1 , 2 , 4 }
126
+ var g = f
127
+
128
+ func init () {
129
+ for i := 4 ; i < n; i++ {
130
+ f[i] = (f[i-1 ] + f[i-2 ] + f[i-3 ]) % mod
131
+ g[i] = (g[i-1 ] + g[i-2 ] + g[i-3 ] + g[i-4 ]) % mod
132
+ }
133
+ }
134
+
135
+ func countTexts (pressedKeys string ) int {
136
+ ans := 1
137
+ for i , j , n := 0 , 0 , len (pressedKeys); i < n; i++ {
138
+ c := pressedKeys[i]
139
+ j = i
140
+ for j+1 < n && pressedKeys[j+1 ] == c {
141
+ j++
142
+ }
143
+ cnt := j - i + 1
144
+ if c == ' 7' || c == ' 9' {
145
+ ans = ans * g[cnt] % mod
146
+ } else {
147
+ ans = ans * f[cnt] % mod
148
+ }
149
+ i = j
150
+ }
151
+ return ans
152
+ }
70
153
```
71
154
72
155
### ** TypeScript**
0 commit comments