Count Number of Texts LeetCode solution in Python
1# Runtime: 2740 ms (Top 30.44%) | Memory: 19.5 MB (Top 65.22%)2class Solution(object):3def countTexts(self, pressedKeys):4"""5:type pressedKeys: str6:rtype: int7"""8dp = [1] + [0] * len(pressedKeys)9mod = 10**9 + 710for i, n in enumerate(pressedKeys):11dp[i + 1] = dp[i]12# check if is continous13if i >= 1 and pressedKeys[i - 1] == n:14dp[i + 1] += dp[i - 1]15dp[i + 1] %= mod16if i >= 2 and pressedKeys[i - 2] == n:17dp[i + 1] += dp[i - 2]18dp[i + 1] %= mod19# Special case for '7' and '9' that can have 4 characters combination20if i >= 3 and pressedKeys[i - 3] == n and (n == "7" or n == "9"):21dp[i + 1] += dp[i - 3]22dp[i + 1] %= mod23return dp[-1]