Count Number of Texts - Complete Solution Guide
Count Number of Texts is LeetCode problem 2266, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Alice is texting Bob using her phone. The mapping of digits to letters is shown in the figure below. In order to add a letter, Alice has to press the key of the corresponding digit i times, where i is the position of the letter in the key. For example, to add the letter 's' , Alice has to press '7' four times. Similarly, to add the letter 'k' , Alice has to press '5' twice. Note that the digits '0' and '1' do not map to any letters, so Alice does not use them. However, due to an error in transmi
Detailed Explanation
The problem asks us to count the number of possible text messages Alice could have sent, given a string of pressed keys Bob received. Each digit from '2' to '9' on the phone keypad corresponds to a set of letters. Pressing a digit multiple times cycles through these letters. For example, '2' can be 'a', 'b', or 'c'. The digits '7' and '9' have four possible letters each. We are given the sequence of pressed keys and must determine how many different combinations of letters could have produced that sequence, returning the result modulo 10^9 + 7.
Solution Approach
The provided code uses dynamic programming to solve this problem. The `dp[i]` value represents the number of possible text messages that can be formed from the first `i` characters of `pressedKeys`. The solution iterates through the `pressedKeys` string, and for each position `i`, it considers the possibilities of pressing the same key 1, 2, 3, or 4 times (only for '7' and '9'). If the previous keys are the same, it adds the corresponding `dp` values to the current `dp[i]` value, ensuring the result is taken modulo 10^9 + 7.
Step-by-Step Algorithm
- Step 1: Initialize a dynamic programming array `dp` of size `n + 1`, where `n` is the length of `pressedKeys`. `dp[0]` is initialized to 1, representing an empty string.
- Step 2: Iterate from `i = 1` to `n`. `dp[i]` is initially set to `dp[i-1]`, representing the case where the current key is pressed only once.
- Step 3: If the current key is the same as the previous key (i.e., `pressedKeys[i-1] == pressedKeys[i-2]`), add `dp[i-2]` to `dp[i]`. This represents the case where the current key is pressed twice.
- Step 4: If the current key is the same as the previous two keys (i.e., `pressedKeys[i-1] == pressedKeys[i-2] == pressedKeys[i-3]`), add `dp[i-3]` to `dp[i]`. This represents the case where the current key is pressed three times.
- Step 5: If the current key is '7' or '9' and is the same as the previous three keys (i.e., `pressedKeys[i-1] == pressedKeys[i-2] == pressedKeys[i-3] == pressedKeys[i-4]`), add `dp[i-4]` to `dp[i]`. This represents the case where the current key is pressed four times.
- Step 6: After each addition, take the modulo `10^9 + 7` to prevent integer overflow.
- Step 7: Finally, return `dp[n]`, which represents the total number of possible text messages.
Key Insights
- Insight 1: Dynamic Programming is suitable because the number of possible texts for a given prefix of `pressedKeys` can be calculated based on the results for shorter prefixes.
- Insight 2: Recognize repeating digit sequences. The problem boils down to counting the combinations within each sequence of identical digits.
- Insight 3: Handle the '7' and '9' keys specially, as they map to four letters instead of three.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Hash Table, Math, String, Dynamic Programming.
Companies
Asked at: Goldman Sachs.