|
| 1 | +--- |
| 2 | +id: letter-combination |
| 3 | +title: Letter Combinations of a Phone Number (LeetCode) |
| 4 | +sidebar_label: 0017 - letter-combination |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Backtracking |
| 8 | + - String |
| 9 | +description: "Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent." |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order. |
| 15 | + |
| 16 | +A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. |
| 17 | + |
| 18 | + |
| 19 | +### Examples |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | + |
| 23 | +``` |
| 24 | +Input |
| 25 | +digits = " |
| 26 | +Output |
| 27 | +["ad","ae","af","bd","be","bf","cd","ce","cf"] |
| 28 | +
|
| 29 | +``` |
| 30 | +**Example 2:** |
| 31 | + |
| 32 | +``` |
| 33 | +Input: digits = "" |
| 34 | +Output: [] |
| 35 | +``` |
| 36 | +**Example 3:** |
| 37 | + |
| 38 | +``` |
| 39 | +Input: digits = "2" |
| 40 | +Output: ["a","b","c"] |
| 41 | +``` |
| 42 | +### Constraints |
| 43 | + |
| 44 | +- `0 <= digits.length <= 4` |
| 45 | +- `digits[i] is a digit in the range ['2', '9']` |
| 46 | + |
| 47 | + |
| 48 | +## Solution for Letter combination of phone number |
| 49 | + |
| 50 | +### Intuition |
| 51 | +Using backtracking to create all possible combinations |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +### Complexity Analysis |
| 57 | + |
| 58 | +#### Time Complexity: $O(3^n)$ |
| 59 | + |
| 60 | +#### Space Complexity: $O(N)$ |
| 61 | + |
| 62 | + |
| 63 | +## Approach |
| 64 | + |
| 65 | +- This is based on a Python solution. Other implementations might differ a bit. |
| 66 | + |
| 67 | +- Initialize an empty list `res` to store the generated combinations. |
| 68 | + |
| 69 | +- Check if the `digits` string is empty. If it is, return an empty list since there are no digits to process. |
| 70 | + |
| 71 | +- Create a dictionary `digit_to_letters` that maps each digit from '2' to '9' to the corresponding letters on a phone keypad. |
| 72 | + |
| 73 | +- Define a recursive function `backtrack(idx, comb)` that takes two parameters: |
| 74 | + - `idx`: The current index of the digit being processed in the `digits` string. |
| 75 | + - `comb`: The current combination being formed by appending letters. |
| 76 | + |
| 77 | +- Inside the `backtrack` function: |
| 78 | + - Check if `idx` is equal to the length of the `digits` string. If it is, it means a valid combination has been formed, so append the current `comb` to the `res` list. |
| 79 | + - If not, iterate through each letter corresponding to the digit at `digits[idx]` using the `digit_to_letters` dictionary. |
| 80 | + - For each letter, recursively call `backtrack` with `idx + 1` to process the next digit and `comb + letter` to add the current letter to the combination. |
| 81 | + |
| 82 | +- Initialize the `res` list. |
| 83 | + |
| 84 | +- Start the initial call to `backtrack` with `idx` set to 0 and an empty string as `comb`. This will start the process of generating combinations. |
| 85 | + |
| 86 | +- After the recursive calls have been made, return the `res` list containing all the generated combinations. |
| 87 | + |
| 88 | +- The algorithm works by iteratively exploring all possible combinations of letters that can be formed from the given input digits. It uses a recursive approach to generate combinations, building them one letter at a time. The base case for the recursion is when all digits have been processed, at which point a combination is complete and added to the `res` list. The backtracking nature of the algorithm ensures that all possible combinations are explored. |
| 89 | + |
| 90 | + |
| 91 | +### Code in Different Languages |
| 92 | + |
| 93 | +<Tabs> |
| 94 | +<TabItem value="cpp" label="C++"> |
| 95 | + <SolutionAuthor name="@Shreyash3087"/> |
| 96 | + |
| 97 | +```cpp |
| 98 | +#include <vector> |
| 99 | +#include <algorithm> |
| 100 | +#include <climits> |
| 101 | + |
| 102 | +class Solution { |
| 103 | +public: |
| 104 | + int cherryPickup(std::vector<std::vector<int>>& grid) { |
| 105 | + int N = grid.size(); |
| 106 | + std::vector<std::vector<int>> dp(N, std::vector<int>(N, INT_MIN)); |
| 107 | + dp[0][0] = grid[0][0]; |
| 108 | + |
| 109 | + for (int t = 1; t <= 2 * N - 2; ++t) { |
| 110 | + std::vector<std::vector<int>> dp2(N, std::vector<int>(N, INT_MIN)); |
| 111 | + |
| 112 | + for (int i = std::max(0, t - (N - 1)); i <= std::min(N - 1, t); ++i) { |
| 113 | + for (int j = std::max(0, t - (N - 1)); j <= std::min(N - 1, t); ++j) { |
| 114 | + if (grid[i][t - i] == -1 || grid[j][t - j] == -1) { |
| 115 | + continue; |
| 116 | + } |
| 117 | + int val = grid[i][t - i]; |
| 118 | + if (i != j) { |
| 119 | + val += grid[j][t - j]; |
| 120 | + } |
| 121 | + for (int pi = i - 1; pi <= i; ++pi) { |
| 122 | + for (int pj = j - 1; pj <= j; ++pj) { |
| 123 | + if (pi >= 0 && pj >= 0) { |
| 124 | + dp2[i][j] = std::max(dp2[i][j], dp[pi][pj] + val); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + dp = std::move(dp2); |
| 131 | + } |
| 132 | + return std::max(0, dp[N - 1][N - 1]); |
| 133 | + } |
| 134 | +}; |
| 135 | + |
| 136 | + |
| 137 | +``` |
| 138 | +</TabItem> |
| 139 | +<TabItem value="java" label="Java"> |
| 140 | + <SolutionAuthor name="@Shreyash3087"/> |
| 141 | + |
| 142 | +```java |
| 143 | +class Solution { |
| 144 | + public int cherryPickup(int[][] grid) { |
| 145 | + int N = grid.length; |
| 146 | + int[][] dp = new int[N][N]; |
| 147 | + for (int[] row: dp) { |
| 148 | + Arrays.fill(row, Integer.MIN_VALUE); |
| 149 | + } |
| 150 | + dp[0][0] = grid[0][0]; |
| 151 | + |
| 152 | + for (int t = 1; t <= 2*N - 2; ++t) { |
| 153 | + int[][] dp2 = new int[N][N]; |
| 154 | + for (int[] row: dp2) { |
| 155 | + Arrays.fill(row, Integer.MIN_VALUE); |
| 156 | + } |
| 157 | + |
| 158 | + for (int i = Math.max(0, t - (N - 1)); i <= Math.min(N - 1, t); ++i) { |
| 159 | + for (int j = Math.max(0, t - (N - 1)); j <= Math.min(N - 1, t); ++j) { |
| 160 | + if (grid[i][t - i] == -1 || grid[j][t - j] == -1) { |
| 161 | + continue; |
| 162 | + } |
| 163 | + int val = grid[i][t-i]; |
| 164 | + if (i != j) { |
| 165 | + val += grid[j][t - j]; |
| 166 | + } |
| 167 | + for (int pi = i - 1; pi <= i; ++pi) { |
| 168 | + for (int pj = j - 1; pj <= j; ++pj) { |
| 169 | + if (pi >= 0 && pj >= 0) { |
| 170 | + dp2[i][j] = Math.max(dp2[i][j], dp[pi][pj] + val); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + dp = dp2; |
| 177 | + } |
| 178 | + return Math.max(0, dp[N - 1][N - 1]); |
| 179 | + } |
| 180 | +} |
| 181 | +``` |
| 182 | + |
| 183 | +</TabItem> |
| 184 | +<TabItem value="python" label="Python"> |
| 185 | + <SolutionAuthor name="@Shreyash3087"/> |
| 186 | + |
| 187 | +```python |
| 188 | +class Solution(object): |
| 189 | + def cherryPickup(self, grid): |
| 190 | + N = len(grid) |
| 191 | + dp = [[float('-inf')] * N for _ in xrange(N)] |
| 192 | + dp[0][0] = grid[0][0] |
| 193 | + for t in xrange(1, 2 * N - 1): |
| 194 | + dp2 = [[float('-inf')] * N for _ in xrange(N)] |
| 195 | + for i in xrange(max(0, t - (N - 1)), min(N - 1, t) + 1): |
| 196 | + for j in xrange(max(0, t - (N - 1)), min(N - 1, t) + 1): |
| 197 | + if grid[i][t - i] == -1 or grid[j][t - j] == -1: |
| 198 | + continue |
| 199 | + val = grid[i][t - i] |
| 200 | + if i != j: |
| 201 | + val += grid[j][t - j] |
| 202 | + dp2[i][j] = max(dp[pi][pj] + val |
| 203 | + for pi in (i - 1, i) for pj in (j - 1, j) |
| 204 | + if pi >= 0 and pj >= 0) |
| 205 | + dp = dp2 |
| 206 | + return max(0, dp[N - 1][N - 1]) |
| 207 | +``` |
| 208 | +</TabItem> |
| 209 | +</Tabs> |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | +## References |
| 214 | + |
| 215 | +- **LeetCode Problem**: [Letter combinatino of Phone number](https://leetcode.com/problems/cherry-pickup/description/) |
| 216 | + |
| 217 | +- **Solution Link**: [letter combination of phone number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/solutions/5125020/video-simple-solution/) |
0 commit comments