2266. Count Number of Texts

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2266

mod = 10**9 + 7
f = [1, 1, 2, 4]
g = [1, 1, 2, 4]
for _ in range(100000):
    f.append((f[-1] + f[-2] + f[-3]) % mod)
    g.append((g[-1] + g[-2] + g[-3] + g[-4]) % mod)


class Solution:
    def countTexts(self, pressedKeys: str) -> int:
        res = 1
        for ch, s in groupby(pressedKeys):
            m = len(list(s))
            res = res * (g[m] if ch in "79" else f[m]) % mod
        return res