Skip to content

Commit 9f2d908

Browse files
committed
feat: add solutions to lc problem: No.2266
No.2266.Count Number of Texts
1 parent 350285e commit 9f2d908

File tree

5 files changed

+248
-2
lines changed

5 files changed

+248
-2
lines changed

solution/2200-2299/2266.Count Number of Texts/README.md

+84-1
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,98 @@ Alice 可能发出的文字信息包括:
6868
<!-- 这里可写当前语言的特殊实现逻辑 -->
6969

7070
```python
71-
71+
mod = 10**9 + 7
72+
f = [1, 1, 2, 4]
73+
g = [1, 1, 2, 4]
74+
for _ in range(100000):
75+
f.append((f[-1] + f[-2] + f[-3]) % mod)
76+
g.append((g[-1] + g[-2] + g[-3] + g[-4]) % mod)
77+
78+
79+
class Solution:
80+
def countTexts(self, pressedKeys: str) -> int:
81+
ans = 1
82+
for ch, s in groupby(pressedKeys):
83+
m = len(list(s))
84+
ans = ans * (g[m] if ch in "79" else f[m]) % mod
85+
return ans
7286
```
7387

7488
### **Java**
7589

7690
<!-- 这里可写当前语言的特殊实现逻辑 -->
7791

7892
```java
93+
class Solution {
94+
private static final int N = 100010;
95+
private static final int MOD = (int) 1e9 + 7;
96+
private static long[] f = new long[N];
97+
private static long[] g = new long[N];
98+
static {
99+
f[0] = 1;
100+
f[1] = 1;
101+
f[2] = 2;
102+
f[3] = 4;
103+
g[0] = 1;
104+
g[1] = 1;
105+
g[2] = 2;
106+
g[3] = 4;
107+
for (int i = 4; i < N; ++i) {
108+
f[i] = (f[i - 1] + f[i - 2] + f[i - 3]) % MOD;
109+
g[i] = (g[i - 1] + g[i - 2] + g[i - 3] + g[i - 4]) % MOD;
110+
}
111+
}
112+
113+
public int countTexts(String pressedKeys) {
114+
long ans = 1;
115+
for (int i = 0, n = pressedKeys.length(); i < n; ++i) {
116+
int j = i;
117+
char c = pressedKeys.charAt(i);
118+
for (; j + 1 < n && pressedKeys.charAt(j + 1) == c; ++j);
119+
int cnt = j - i + 1;
120+
ans = c == '7' || c == '9' ? ans * g[cnt] : ans * f[cnt];
121+
ans %= MOD;
122+
i = j;
123+
}
124+
return (int) ans;
125+
}
126+
}
127+
```
79128

129+
### **Go**
130+
131+
```go
132+
const mod int = 1e9 + 7
133+
const n int = 1e5 + 10
134+
135+
var f = [n]int{1, 1, 2, 4}
136+
var g = f
137+
138+
func init() {
139+
for i := 4; i < n; i++ {
140+
f[i] = (f[i-1] + f[i-2] + f[i-3]) % mod
141+
g[i] = (g[i-1] + g[i-2] + g[i-3] + g[i-4]) % mod
142+
}
143+
}
144+
145+
func countTexts(pressedKeys string) int {
146+
ans := 1
147+
for i, j, n := 0, 0, len(pressedKeys); i < n; i++ {
148+
c := pressedKeys[i]
149+
j = i
150+
for j+1 < n && pressedKeys[j+1] == c {
151+
j++
152+
}
153+
cnt := j - i + 1
154+
if c == '7' || c == '9' {
155+
ans = ans * g[cnt] % mod
156+
} else {
157+
ans = ans * f[cnt] % mod
158+
}
159+
i = j
160+
}
161+
return ans
162+
}
80163
```
81164

82165
### **TypeScript**

solution/2200-2299/2266.Count Number of Texts/README_EN.md

+84-1
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,96 @@ Since we need to return the answer modulo 10<sup>9</sup> + 7, we return 20828761
6060
### **Python3**
6161

6262
```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
6478
```
6579

6680
### **Java**
6781

6882
```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+
```
69118

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+
}
70153
```
71154

72155
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const mod int = 1e9 + 7
2+
const n int = 1e5 + 10
3+
4+
var f = [n]int{1, 1, 2, 4}
5+
var g = f
6+
7+
func init() {
8+
for i := 4; i < n; i++ {
9+
f[i] = (f[i-1] + f[i-2] + f[i-3]) % mod
10+
g[i] = (g[i-1] + g[i-2] + g[i-3] + g[i-4]) % mod
11+
}
12+
}
13+
14+
func countTexts(pressedKeys string) int {
15+
ans := 1
16+
for i, j, n := 0, 0, len(pressedKeys); i < n; i++ {
17+
c := pressedKeys[i]
18+
j = i
19+
for j+1 < n && pressedKeys[j+1] == c {
20+
j++
21+
}
22+
cnt := j - i + 1
23+
if c == '7' || c == '9' {
24+
ans = ans * g[cnt] % mod
25+
} else {
26+
ans = ans * f[cnt] % mod
27+
}
28+
i = j
29+
}
30+
return ans
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
private static final int N = 100010;
3+
private static final int MOD = (int) 1e9 + 7;
4+
private static long[] f = new long[N];
5+
private static long[] g = new long[N];
6+
static {
7+
f[0] = 1;
8+
f[1] = 1;
9+
f[2] = 2;
10+
f[3] = 4;
11+
g[0] = 1;
12+
g[1] = 1;
13+
g[2] = 2;
14+
g[3] = 4;
15+
for (int i = 4; i < N; ++i) {
16+
f[i] = (f[i - 1] + f[i - 2] + f[i - 3]) % MOD;
17+
g[i] = (g[i - 1] + g[i - 2] + g[i - 3] + g[i - 4]) % MOD;
18+
}
19+
}
20+
21+
public int countTexts(String pressedKeys) {
22+
long ans = 1;
23+
for (int i = 0, n = pressedKeys.length(); i < n; ++i) {
24+
int j = i;
25+
char c = pressedKeys.charAt(i);
26+
for (; j + 1 < n && pressedKeys.charAt(j + 1) == c; ++j);
27+
int cnt = j - i + 1;
28+
ans = c == '7' || c == '9' ? ans * g[cnt] : ans * f[cnt];
29+
ans %= MOD;
30+
i = j;
31+
}
32+
return (int) ans;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
mod = 10**9 + 7
2+
f = [1, 1, 2, 4]
3+
g = [1, 1, 2, 4]
4+
for _ in range(100000):
5+
f.append((f[-1] + f[-2] + f[-3]) % mod)
6+
g.append((g[-1] + g[-2] + g[-3] + g[-4]) % mod)
7+
8+
9+
class Solution:
10+
def countTexts(self, pressedKeys: str) -> int:
11+
ans = 1
12+
for ch, s in groupby(pressedKeys):
13+
m = len(list(s))
14+
ans = ans * (g[m] if ch in "79" else f[m]) % mod
15+
return ans

0 commit comments

Comments
 (0)