Leetcode Problem 2266. Count Number of Texts

2266. Count Number of Texts

Leetcode Solutions

Dynamic Programming with Modulo Arithmetic

  1. Define a constant MOD with the value 10^9 + 7 for modulo operations.
  2. Initialize a DP array dp with a size of len(pressedKeys) + 1 and set dp[0] to 1.
  3. Iterate over the pressedKeys string from index 1 to len(pressedKeys).
  4. For each index i, set dp[i] to dp[i-1] modulo MOD.
  5. If the current key is the same as the previous key, add dp[i-2] to dp[i].
  6. If the current key is the same as the two previous keys, add dp[i-3] to dp[i].
  7. If the current key is '7' or '9' and is the same as the three previous keys, add dp[i-4] to dp[i].
  8. Apply modulo MOD to dp[i] after each addition.
  9. Return dp[len(pressedKeys)] as the final answer.
UML Thumbnail

Recursive Approach with Memoization

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...