|
| 1 | +--- |
| 2 | +id: Count-Number-of-Texts |
| 3 | +title: Count Number of Texts |
| 4 | +sidebar_label: 2266-Count Number of Texts |
| 5 | +tags: |
| 6 | + - Hash table |
| 7 | + - Brute Force |
| 8 | + - Optimized approach |
| 9 | + - LeetCode |
| 10 | + - Python |
| 11 | + - Java |
| 12 | + - C++ |
| 13 | + |
| 14 | +description: "This is a solution to Count Number of Texts problem on LeetCode." |
| 15 | +sidebar_position: 67 |
| 16 | +--- |
| 17 | + |
| 18 | +## Problem Statement |
| 19 | +In this tutorial, we will solve the Count Number of Texts problem . We will provide the implementation of the solution in Python, Java, and C++. |
| 20 | + |
| 21 | +### Problem Description |
| 22 | + |
| 23 | +Alice is texting Bob using her phone. The mapping of digits to letters is shown in the figure below. |
| 24 | + |
| 25 | + |
| 26 | +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. |
| 27 | + |
| 28 | +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. |
| 29 | +Note that the digits '0' and '1' do not map to any letters, so Alice does not use them. |
| 30 | +However, due to an error in transmission, Bob did not receive Alice's text message but received a string of pressed keys instead. |
| 31 | + |
| 32 | +For example, when Alice sent the message "bob", Bob received the string "2266622". |
| 33 | +Given a string pressedKeys representing the string received by Bob, return the total number of possible text messages Alice could have sent. |
| 34 | + |
| 35 | +Since the answer may be very large, return it modulo 109 + 7. |
| 36 | + |
| 37 | + |
| 38 | +### Examples |
| 39 | + |
| 40 | +**Example 1:** |
| 41 | +Input: pressedKeys = "22233" |
| 42 | +Output: 8 |
| 43 | +Explanation: |
| 44 | +The possible text messages Alice could have sent are: |
| 45 | +"aaadd", "abdd", "badd", "cdd", "aaae", "abe", "bae", and "ce". |
| 46 | +Since there are 8 possible messages, we return 8. |
| 47 | +**Example 2:** |
| 48 | +Input: pressedKeys = "222222222222222222222222222222222222" |
| 49 | +Output: 82876089 |
| 50 | +Explanation: |
| 51 | +There are 2082876103 possible text messages Alice could have sent. |
| 52 | +Since we need to return the answer modulo 109 + 7, we return 2082876103 % (109 + 7) = 82876089. |
| 53 | +### Constraints |
| 54 | +- `1 <= pressedKeys.length <= 105` |
| 55 | +- `pressedKeys only consists of digits from '2' - '9'.` |
| 56 | +## Solution of Given Problem |
| 57 | + |
| 58 | +### Intuition and Approach |
| 59 | + |
| 60 | +The problem can be solved using a brute force approach or an optimized Technique. |
| 61 | + |
| 62 | +<Tabs> |
| 63 | +<tabItem value="Brute Force" label="Brute Force"> |
| 64 | + |
| 65 | +### Approach 1:Brute Force (Naive) |
| 66 | + |
| 67 | + |
| 68 | +In the brute force approach, we will generate all possible combinations of letters for the given string of pressed keys and count the valid messages. However, due to the constraints, this approach is impractical for large inputs. |
| 69 | + |
| 70 | +#### Codes in Different Languages |
| 71 | + |
| 72 | +<Tabs> |
| 73 | +<TabItem value="C++" label="C++" default> |
| 74 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 75 | + |
| 76 | +```cpp |
| 77 | +#include <iostream> |
| 78 | +#include <string> |
| 79 | +#include <vector> |
| 80 | +using namespace std; |
| 81 | + |
| 82 | +const int MOD = 1e9 + 7; |
| 83 | +vector<string> mappings = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; |
| 84 | + |
| 85 | +void generateCombinations(const string &keys, int index, string current, vector<string> &results) { |
| 86 | + if (index == keys.size()) { |
| 87 | + results.push_back(current); |
| 88 | + return; |
| 89 | + } |
| 90 | + int digit = keys[index] - '2'; |
| 91 | + for (char c : mappings[digit]) { |
| 92 | + generateCombinations(keys, index + 1, current + c, results); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +int bruteForce(const string &pressedKeys) { |
| 97 | + vector<string> results; |
| 98 | + generateCombinations(pressedKeys, 0, "", results); |
| 99 | + return results.size(); |
| 100 | +} |
| 101 | + |
| 102 | +int main() { |
| 103 | + string pressedKeys = "22233"; |
| 104 | + cout << bruteForce(pressedKeys) << endl; // Output: 8 |
| 105 | + return 0; |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +``` |
| 110 | +</TabItem> |
| 111 | +<TabItem value="Java" label="Java"> |
| 112 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 113 | +
|
| 114 | +```java |
| 115 | +import java.util.*; |
| 116 | +
|
| 117 | +public class Main { |
| 118 | + static final int MOD = 1_000_000_007; |
| 119 | + static final String[] mappings = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; |
| 120 | +
|
| 121 | + public static void generateCombinations(String keys, int index, String current, List<String> results) { |
| 122 | + if (index == keys.length()) { |
| 123 | + results.add(current); |
| 124 | + return; |
| 125 | + } |
| 126 | + int digit = keys.charAt(index) - '2'; |
| 127 | + for (char c : mappings[digit].toCharArray()) { |
| 128 | + generateCombinations(keys, index + 1, current + c, results); |
| 129 | + } |
| 130 | + } |
| 131 | +
|
| 132 | + public static int bruteForce(String pressedKeys) { |
| 133 | + List<String> results = new ArrayList<>(); |
| 134 | + generateCombinations(pressedKeys, 0, "", results); |
| 135 | + return results.size(); |
| 136 | + } |
| 137 | +
|
| 138 | + public static void main(String[] args) { |
| 139 | + String pressedKeys = "22233"; |
| 140 | + System.out.println(bruteForce(pressedKeys)); // Output: 8 |
| 141 | + } |
| 142 | +} |
| 143 | +
|
| 144 | +
|
| 145 | +``` |
| 146 | + |
| 147 | + |
| 148 | +</TabItem> |
| 149 | +<TabItem value="Python" label="Python"> |
| 150 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 151 | + |
| 152 | +```python |
| 153 | +mappings = ["abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"] |
| 154 | + |
| 155 | +def generate_combinations(keys, index, current, results): |
| 156 | + if index == len(keys): |
| 157 | + results.append(current) |
| 158 | + return |
| 159 | + digit = int(keys[index]) - 2 |
| 160 | + for c in mappings[digit]: |
| 161 | + generate_combinations(keys, index + 1, current + c, results) |
| 162 | + |
| 163 | +def brute_force(pressed_keys): |
| 164 | + results = [] |
| 165 | + generate_combinations(pressed_keys, 0, "", results) |
| 166 | + return len(results) |
| 167 | + |
| 168 | +pressed_keys = "22233" |
| 169 | +print(brute_force(pressed_keys)) # Output: 8 |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | +``` |
| 174 | + |
| 175 | +</TabItem> |
| 176 | +</Tabs> |
| 177 | + |
| 178 | + |
| 179 | +### Complexity Analysis |
| 180 | + |
| 181 | +- Time Complexity: $O(3^n)$ or $O(4^n)$ |
| 182 | +- due to generating all possible letter combinations for each key press sequence. |
| 183 | +- Space Complexity: $O(3^n)$ or $O(4^n)$ |
| 184 | +- because we store all generated combinations. |
| 185 | + |
| 186 | +</tabItem> |
| 187 | +<tabItem value="Optimized approach" label="Optimized approach"> |
| 188 | + |
| 189 | +### Approach 2: Optimized approach |
| 190 | + |
| 191 | +The optimized approach uses dynamic programming to efficiently calculate the number of valid messages. We maintain a DP array where dp[i] represents the number of possible messages for the substring pressedKeys[0:i+1]. |
| 192 | + |
| 193 | + |
| 194 | +#### Code in Different Languages |
| 195 | + |
| 196 | +<Tabs> |
| 197 | +<TabItem value="C++" label="C++" default> |
| 198 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 199 | + |
| 200 | +```cpp |
| 201 | +#include <iostream> |
| 202 | +#include <string> |
| 203 | +#include <vector> |
| 204 | +using namespace std; |
| 205 | + |
| 206 | +const int MOD = 1e9 + 7; |
| 207 | + |
| 208 | +int optimizedCount(const string &pressedKeys) { |
| 209 | + int n = pressedKeys.size(); |
| 210 | + vector<long> dp(n + 1, 0); |
| 211 | + dp[0] = 1; |
| 212 | + |
| 213 | + for (int i = 1; i <= n; ++i) { |
| 214 | + dp[i] = dp[i - 1]; |
| 215 | + if (i > 1 && pressedKeys[i - 1] == pressedKeys[i - 2]) |
| 216 | + dp[i] = (dp[i] + dp[i - 2]) % MOD; |
| 217 | + if (i > 2 && pressedKeys[i - 1] == pressedKeys[i - 2] && pressedKeys[i - 2] == pressedKeys[i - 3]) |
| 218 | + dp[i] = (dp[i] + dp[i - 3]) % MOD; |
| 219 | + if (i > 3 && pressedKeys[i - 1] == pressedKeys[i - 2] && pressedKeys[i - 2] == pressedKeys[i - 3] && pressedKeys[i - 3] == pressedKeys[i - 4] && (pressedKeys[i - 1] == '7' || pressedKeys[i - 1] == '9')) |
| 220 | + dp[i] = (dp[i] + dp[i - 4]) % MOD; |
| 221 | + } |
| 222 | + return dp[n]; |
| 223 | +} |
| 224 | + |
| 225 | +int main() { |
| 226 | + string pressedKeys = "22233"; |
| 227 | + cout << optimizedCount(pressedKeys) << endl; // Output: 8 |
| 228 | + return 0; |
| 229 | +} |
| 230 | + |
| 231 | + |
| 232 | + |
| 233 | +``` |
| 234 | +</TabItem> |
| 235 | +<TabItem value="Java" label="Java"> |
| 236 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 237 | +
|
| 238 | +```java |
| 239 | +public class Main { |
| 240 | + static final int MOD = 1_000_000_007; |
| 241 | +
|
| 242 | + public static int optimizedCount(String pressedKeys) { |
| 243 | + int n = pressedKeys.length(); |
| 244 | + long[] dp = new long[n + 1]; |
| 245 | + dp[0] = 1; |
| 246 | +
|
| 247 | + for (int i = 1; i <= n; ++i) { |
| 248 | + dp[i] = dp[i - 1]; |
| 249 | + if (i > 1 && pressedKeys.charAt(i - 1) == pressedKeys.charAt(i - 2)) |
| 250 | + dp[i] = (dp[i] + dp[i - 2]) % MOD; |
| 251 | + if (i > 2 && pressedKeys.charAt(i - 1) == pressedKeys.charAt(i - 2) && pressedKeys.charAt(i - 2) == pressedKeys.charAt(i - 3)) |
| 252 | + dp[i] = (dp[i] + dp[i - 3]) % MOD; |
| 253 | + if (i > 3 && pressedKeys.charAt(i - 1) == pressedKeys.charAt(i - 2) && pressedKeys.charAt(i - 2) == pressedKeys.charAt(i - 3) && pressedKeys.charAt(i - 3) == pressedKeys.charAt(i - 4) && (pressedKeys.charAt(i - 1) == '7' || pressedKeys.charAt(i - 1) == '9')) |
| 254 | + dp[i] = (dp[i] + dp[i - 4]) % MOD; |
| 255 | + } |
| 256 | + return (int) dp[n]; |
| 257 | + } |
| 258 | +
|
| 259 | + public static void main(String[] args) { |
| 260 | + String pressedKeys = "22233"; |
| 261 | + System.out.println(optimizedCount(pressedKeys)); // Output: 8 |
| 262 | + } |
| 263 | +} |
| 264 | +
|
| 265 | +
|
| 266 | +``` |
| 267 | + |
| 268 | + |
| 269 | +</TabItem> |
| 270 | +<TabItem value="Python" label="Python"> |
| 271 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 272 | + |
| 273 | +```python |
| 274 | +MOD = 1_000_000_007 |
| 275 | + |
| 276 | +def optimized_count(pressed_keys): |
| 277 | + n = len(pressed_keys) |
| 278 | + dp = [0] * (n + 1) |
| 279 | + dp[0] = 1 |
| 280 | + |
| 281 | + for i in range(1, n + 1): |
| 282 | + dp[i] = dp[i - 1] |
| 283 | + if i > 1 and pressed_keys[i - 1] == pressed_keys[i - 2]: |
| 284 | + dp[i] = (dp[i] + dp[i - 2]) % MOD |
| 285 | + if i > 2 and pressed_keys[i - 1] == pressed_keys[i - 2] == pressed_keys[i - 3]: |
| 286 | + dp[i] = (dp[i] + dp[i - 3]) % MOD |
| 287 | + if i > 3 and pressed_keys[i - 1] == pressed_keys[i - 2] == pressed_keys[i - 3] == pressed_keys[i - 4] and pressed_keys[i - 1] in '79': |
| 288 | + dp[i] = (dp[i] + dp[i - 4]) % MOD |
| 289 | + |
| 290 | + return dp[n] |
| 291 | + |
| 292 | +pressed_keys = "22233" |
| 293 | +print(optimized_count(pressed_keys)) # Output: 8 |
| 294 | + |
| 295 | + |
| 296 | + |
| 297 | +``` |
| 298 | + |
| 299 | +</TabItem> |
| 300 | +</Tabs> |
| 301 | + |
| 302 | +#### Complexity Analysis |
| 303 | + |
| 304 | +- Time Complexity: $O(n)$ |
| 305 | +- as we use a dynamic programming approach with a single pass through the input string. |
| 306 | +- Space Complexity: $O(n)$ |
| 307 | +- for storing the DP array. |
| 308 | +</tabItem> |
| 309 | +</Tabs> |
| 310 | + |
| 311 | +--- |
| 312 | + |
| 313 | +<h2>Authors:</h2> |
| 314 | + |
| 315 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 316 | +{['AmruthaPariprolu'].map(username => ( |
| 317 | + <Author key={username} username={username} /> |
| 318 | +))} |
| 319 | +</div> |
0 commit comments