虽然处理了很多次索引思路,不过最后还是过了。第一眼就有解题思路,这种感觉真不错,要的就是这种打怪升级的正反馈。
附上解题代码
`#
@lc app=leetcode.cn id=2266 lang=python3
[2266] 统计打字方案数
@lc code=start
from collections import Counter
from functools import cache
class Solution:
def countTexts(self, pressedKeys: str) -> int:
MOD = 10**9 + 7
n = len(pressedKeys)
dp_3 = [0] * n
# 用于存储连续按同样数字时的情况
# 分为按键有四个字母和三个字母两种情况
dp_4 = [0] * n
if n >= 1:
dp_3[0] = 1
if n >= 2:
dp_3[1] = 2
if n >= 3:
dp_3[2] = 4
if n >= 1:
dp_4[0] = 1
if n >= 2:
dp_4[1] = 2
if n >= 3:
dp_4[2] = 4
if n >= 4:
dp_4[3] = 8
ans = 1
button_3 = {'2','3','4','5','6','8'}
button_4 = {'7','9'}
for i in range(3, n):
dp_3[i] = (dp_3[i - 1] + dp_3[i - 2] + dp_3[i - 3]) % MOD
for i in range(4, n):dp_4[i] = (dp_4[i - 1] + dp_4[i - 2] + dp_4[i - 3] + dp_4[i - 4]) % MODcount = 1for i in range(1,n):if pressedKeys[i] == pressedKeys[i - 1]:count += 1else:if pressedKeys[i - 1] in button_3:ans = (ans * dp_3[count - 1]) % MODelse:ans = (ans * dp_4[count - 1]) % MODcount = 1if pressedKeys[-1] in button_3:ans = (ans * dp_3[count - 1]) % MODelse:ans = (ans * dp_4[count - 1]) % MODreturn ans`