1class Solution:
2 def countTexts(self, pressedKeys: str) -> int:
3 dp = [0 for _ in range(len(pressedKeys)+1)]
4 dp[0] = 1
5 for i in range(1, len(pressedKeys) + 1):
6 dp[i] = dp[i-1]
7
8 if i >= 2:
9 if pressedKeys[i-1] == pressedKeys[i-2]:
10 dp[i] += dp[i-2]
11
12 if i >= 3:
13 if pressedKeys[i-1] == pressedKeys[i-2] == pressedKeys[i-3]:
14 dp[i] += dp[i-3]
15
16 if i>= 4:
17 if pressedKeys[i-1] == pressedKeys[i-2] == pressedKeys[i-3] == pressedKeys[i-4]:
18 if pressedKeys[i-1] in ["7","9"]:
19 dp[i] += dp[i-4]
20
21 return dp[len(pressedKeys)] % (10**9 + 7)
Solution to "Second Minimum Node In a Binary Tree" on Leetcode.