Skip to content

Commit 014ea99

Browse files
committed
Leetcode | Count Number of Texts | Java
1 parent af04fe9 commit 014ea99

4 files changed

Lines changed: 86 additions & 0 deletions

File tree

304 KB
Loading
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# 2266. Count Number of Texts
2+
3+
Alice is texting Bob using her phone. The mapping of digits to letters is shown in the figure below.
4+
5+
[Image](1200px-telephone-keypad2svg.png)
6+
7+
In order to add a letter, Alice has to press the key of the corresponding digit i times, where i is the position of the
8+
letter in the key.
9+
10+
- For example, to add the letter `'s'`, Alice has to press `'7'` four times. Similarly, to add the letter `'k'`, Alice
11+
has to press `'5'` twice.
12+
- Note that the digits `'0'` and `'1'` do not map to any letters, so Alice does not use them.
13+
14+
However, due to an error in transmission, Bob did not receive Alice's text message but received a string of pressed keys
15+
instead.
16+
17+
- For example, when Alice sent the message `"bob"`, Bob received the string `"2266622"`.
18+
19+
Given a string `pressedKeys` representing the string received by Bob, return the total number of possible text messages
20+
Alice could have sent.
21+
22+
Since the answer may be very large, return it modulo `10^9 + 7`.
23+
24+
Example 1:
25+
26+
Input: pressedKeys = "22233"
27+
Output: 8
28+
Explanation:
29+
The possible text messages Alice could have sent are:
30+
"aaadd", "abdd", "badd", "cdd", "aaae", "abe", "bae", and "ce".
31+
Since there are 8 possible messages, we return 8.
32+
33+
Example 2:
34+
35+
Input: pressedKeys = "222222222222222222222222222222222222"
36+
Output: 82876089
37+
Explanation:
38+
There are 2082876103 possible text messages Alice could have sent.
39+
Since we need to return the answer modulo 109 + 7, we return 2082876103 % (109 + 7) = 82876089.
40+
41+
Constraints:
42+
43+
- `1 <= pressedKeys.length <= 10^5`
44+
- `pressedKeys` only consists of digits from `'2'` - `'9'`.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.devstromo.medium.p2266;
2+
3+
public class Solution {
4+
private static final int MOD = 1_000_000_007;
5+
6+
public int countTexts(String pressedKeys) {
7+
int n = pressedKeys.length();
8+
int[] dp = new int[n + 1];
9+
dp[0] = 1;
10+
11+
for (int i = 1; i <= n; i++) {
12+
char currentChar = pressedKeys.charAt(i - 1);
13+
dp[i] = 0;
14+
int maxPress = (currentChar == '7' || currentChar == '9') ? 4 : 3;
15+
16+
for (int j = 1; j <= maxPress && i - j >= 0; j++) {
17+
if (pressedKeys.charAt(i - j) != currentChar) break;
18+
dp[i] = (dp[i] + dp[i - j]) % MOD;
19+
}
20+
}
21+
22+
return dp[n];
23+
}
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.devstromo.medium.p2266;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
class SolutionUnitTest {
9+
private final Solution solution = new Solution();
10+
11+
@Test
12+
@DisplayName("Test Count Number of Texts")
13+
void testCountNumberOfTexts() {
14+
assertEquals(8, solution.countTexts("22233"));
15+
assertEquals(82876089, solution.countTexts("222222222222222222222222222222222222"));
16+
17+
}
18+
}

0 commit comments

Comments
 (0)