Count Number of Texts LeetCode solution in Python

1
# Runtime: 2740 ms (Top 30.44%) | Memory: 19.5 MB (Top 65.22%)
2
class Solution(object):
3
def countTexts(self, pressedKeys):
4
"""
5
:type pressedKeys: str
6
:rtype: int
7
"""
8
dp = [1] + [0] * len(pressedKeys)
9
mod = 10**9 + 7
10
for i, n in enumerate(pressedKeys):
11
dp[i + 1] = dp[i]
12
# check if is continous
13
if i >= 1 and pressedKeys[i - 1] == n:
14
dp[i + 1] += dp[i - 1]
15
dp[i + 1] %= mod
16
if i >= 2 and pressedKeys[i - 2] == n:
17
dp[i + 1] += dp[i - 2]
18
dp[i + 1] %= mod
19
# Special case for '7' and '9' that can have 4 characters combination
20
if i >= 3 and pressedKeys[i - 3] == n and (n == "7" or n == "9"):
21
dp[i + 1] += dp[i - 3]
22
dp[i + 1] %= mod
23
return dp[-1]

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0