Easy understanding code in Python with 16 lines
\nclass Solution:\n    def countTexts(self, pressedKeys: str) -> int:\n        MOD = 10 ** 9 + 7\n        dp = [1] + [0] * len(pressedKeys)\n        prev, c = N
```\nclass Solution:\n    def countTexts(self, pressedKeys: str) -> int:\n        MOD = 10 ** 9 + 7\n        dp = [1] + [0] * len(pressedKeys)\n        prev, c = None, 0\n        for i, key in enumerate(pressedKeys):\n            if key == prev:\n                digits = 4 if prev in \'79\' else 3\n                start = i + 1 - min(digits, c + 1) # c + 1 because in 22233 case, when we look at the last 3, we need to look at the combinations at last \'2\' and first \'3\' so look back c + 1\n                dp[i+1] = sum(dp[start : i + 1]) % MOD  # i + 1 because we have a table of len + 1. and we are filling at i + 1 index.\n                c += 1\n            else:\n                dp[i+1] = dp[i] % MOD\n                c = 1\n            prev = key\n        return dp[-1] % MOD\n```\nFirst post, hope you enjoy it.\n\n\n\n\nI would also like to share a ugly but efficient code with a very simple idea(beats 97.71%):\n```\nclass Solution:\n    def countTexts(self, pressedKeys: str) -> int:\n        MOD = 10 ** 9 + 7\n        chr3 = []\n        chr4 = []\n        prev = pressedKeys[0]\n        c = 1\n        \n        for key in pressedKeys[1:]:\n            if key == prev:\n                c += 1\n            else:\n                if prev in {\'7\', \'9\'}:\n                    chr4.append(c)\n                else:\n                    chr3.append(c)\n                c = 1\n            prev = key\n        if prev in {\'7\', \'9\'}:\n            chr4.append(c)\n        else:\n            chr3.append(c)\n        \n        def getCounts(num, digits):\n            dp = [1] + [0] * num\n            for i in range(1, num + 1):\n                start = i - digits\n                dp[i] = sum(dp[start if start >=0 else 0 : i])%MOD\n            return dp\n        \n        res = 1\n        if chr3:\n            max_counts3 = max(chr3)\n            dp_3digs = getCounts(max_counts3, 3)\n            for count in chr3:\n                res *= dp_3digs[count]%MOD\n        if chr4:\n            max_counts4 = max(chr4)\n            dp_4digs = getCounts(max_counts4, 4)\n            for count in chr4:\n                res *= dp_4digs[count]%MOD\n        MOD = 10**9+7\n        return res%MOD\n```\nThe code looks long, but the main idea is simple.\nIf we have 22222223333444, we find it\'s seven \'2\', four \'3\', three \'4\'.\nWe simply return the combinations with 7 repeating numbers x 4 repeating numbers x 3 repeating numbers, without considering what\'s the number.\nThen construct a dp table with the longest repeating int size, which is 7 here.\nwe return dp[7] * dp[3] * dp[4] and only generate a dp table of size 7!\n\nIf we have 7 or 9, simply construct another dp table and others remains the same.\n\n\n\n
['Dynamic Programming', 'Python']