Skip to content

Commit 1863f5d

Browse files
committed
Leetcode | Count Number of Texts | Kotlin
1 parent 75e488a commit 1863f5d

4 files changed

Lines changed: 82 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+
class SolutionKt {
4+
private val mod = 1_000_000_007
5+
6+
fun countTexts(pressedKeys: String): Int {
7+
val n = pressedKeys.length
8+
val dp = IntArray(n + 1)
9+
dp[0] = 1
10+
11+
for (i in 1..n) {
12+
val currentChar = pressedKeys[i - 1]
13+
val maxPress = if (currentChar == '7' || currentChar == '9') 4 else 3
14+
15+
for (j in 1..maxPress) {
16+
if (i - j < 0) break
17+
if (pressedKeys[i - j] != currentChar) break
18+
dp[i] = (dp[i] + dp[i - j]) % mod
19+
}
20+
}
21+
22+
return dp[n]
23+
}
24+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.devstromo.medium.p2266
2+
3+
import org.junit.jupiter.api.Assertions
4+
import org.junit.jupiter.api.Test
5+
6+
class SolutionKtUnitTest {
7+
private val solution = SolutionKt()
8+
9+
@Test
10+
fun `Test Count Number of Texts`() {
11+
Assertions.assertEquals(8, solution.countTexts("22233"))
12+
Assertions.assertEquals(82876089, solution.countTexts("222222222222222222222222222222222222"))
13+
}
14+
}

0 commit comments

Comments
 (0)