Skip to content

Commit 60b2935

Browse files
committed
updated changes
1 parent 0cba8c1 commit 60b2935

File tree

1 file changed

+77
-67
lines changed

1 file changed

+77
-67
lines changed

dsa-solutions/lc-solutions/0000-0099/0017-letter-combinations-of-a-phone-number.md

Lines changed: 77 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
---
2-
id: Letter Combinations of a Phone Number
2+
id: letter-combinations-of-a-phone-number
33
title: Letter Combinations of a Phone Number (LeetCode)
4-
sidebar_label: 0017-Letter-Combinations-of-a-Phone-Number
4+
sidebar_label: 0017-Letter Combinations of a Phone Number
55
tags:
6-
- Back Tracking
7-
- Mapping
8-
- String
6+
- Back Tracking
7+
- Mapping
8+
- String
99
description: The problem requires generating all letter combinations corresponding to given digits (2-9). The solution utilizes backtracking to explore all combinations efficiently, employing a recursive approach in Java.
1010
---
1111

1212
## Problem Description
1313

14-
| Problem Statement | Solution Link | LeetCode Profile |
15-
| :----------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------- |
16-
| [Letter Combinations of a Phone Number](https://leetcode.com/problems/Letter Combinations of a Phone Number/) | [Letter Combinations of a Phone Number Solution on LeetCode](https://leetcode.com/problems/Letter Combinations of a Phone Number/solutions/5055810/video-two-pointer-solution/) | [gabaniyash846](https://leetcode.com/u/gabaniyash846/) |
14+
| Problem Statement | Solution Link | LeetCode Profile |
15+
| :------------------------------------------------------------------------------------------------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :----------------------------------------------------- |
16+
| [Letter Combinations of a Phone Number](https://leetcode.com/problems/Letter Combinations of a Phone Number/) | [Letter Combinations of a Phone Number Solution on LeetCode](https://leetcode.com/problems/Letter Combinations of a Phone Number/solutions/5055810/video-two-pointer-solution/) | [gabaniyash846](https://leetcode.com/u/gabaniyash846/) |
1717

1818
### Problem Description
1919

2020
## Problem Statement:
21+
2122
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.
2223

2324
### Examples
@@ -32,13 +33,13 @@ Given a string containing digits from 2-9 inclusive, return all possible letter
3233
- **Input:** `digits = ""`
3334
- **Output:** `[]`
3435

35-
3636
#### Example 3
3737

3838
- **Input:** `2`
3939
- **Output:** `["a","b","c"]`
4040

4141
### Constraints:
42+
4243
- `0 ≤ digits.length ≤ 4`
4344
- `0 ≤ digits.length ≤ 4digits[𝑖]`
4445
- `digits[i] is a digit in the range ['2', '9'].`
@@ -47,9 +48,11 @@ Given a string containing digits from 2-9 inclusive, return all possible letter
4748
### Approach
4849

4950
1. **Mapping Digits to Letters:**
51+
5052
- Define a mapping of digits to their corresponding letters, similar to telephone buttons.
5153

5254
2. **Backtracking Function:**
55+
5356
- Define a recursive backtracking function to generate all possible combinations.
5457
- The function takes four parameters:
5558
- `index`: The current index in the digits string.
@@ -59,6 +62,7 @@ Given a string containing digits from 2-9 inclusive, return all possible letter
5962
- After the recursive call, we remove the last character from the combination (backtracking).
6063

6164
3. **Base Case:**
65+
6266
- If the length of the current combination is equal to the length of the input digits string, we add the combination to the result list.
6367

6468
4. **Main Function:**
@@ -154,6 +158,7 @@ public class Solution {
154158
```
155159

156160
#### CPP:
161+
157162
```cpp
158163
#include <iostream>
159164
#include <vector>
@@ -209,80 +214,82 @@ int main() {
209214
```
210215
211216
#### JavaScript
217+
212218
```js
213219
/**
214220
* @param {string} digits
215221
* @return {string[]}
216222
*/
217-
var letterCombinations = function(digits) {
218-
if (digits.length === 0) return [];
219-
220-
const digitToLetters = {
221-
'2': 'abc',
222-
'3': 'def',
223-
'4': 'ghi',
224-
'5': 'jkl',
225-
'6': 'mno',
226-
'7': 'pqrs',
227-
'8': 'tuv',
228-
'9': 'wxyz'
229-
};
230-
231-
const combinations = [];
232-
233-
const backtrack = (index, path) => {
234-
if (index === digits.length) {
235-
combinations.push(path);
236-
return;
237-
}
238-
const letters = digitToLetters[digits.charAt(index)];
239-
for (let letter of letters) {
240-
backtrack(index + 1, path + letter);
241-
}
242-
};
243-
244-
backtrack(0, '');
245-
return combinations;
223+
var letterCombinations = function (digits) {
224+
if (digits.length === 0) return [];
225+
226+
const digitToLetters = {
227+
2: "abc",
228+
3: "def",
229+
4: "ghi",
230+
5: "jkl",
231+
6: "mno",
232+
7: "pqrs",
233+
8: "tuv",
234+
9: "wxyz",
235+
};
236+
237+
const combinations = [];
238+
239+
const backtrack = (index, path) => {
240+
if (index === digits.length) {
241+
combinations.push(path);
242+
return;
243+
}
244+
const letters = digitToLetters[digits.charAt(index)];
245+
for (let letter of letters) {
246+
backtrack(index + 1, path + letter);
247+
}
248+
};
249+
250+
backtrack(0, "");
251+
return combinations;
246252
};
247253
248254
// Example usage:
249255
console.log(letterCombinations("23")); // Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
250256
```
251257

252258
#### TypeScript
259+
253260
```ts
254261
class Solution {
255-
private digitToLetters: { [key: string]: string } = {
256-
'2': 'abc',
257-
'3': 'def',
258-
'4': 'ghi',
259-
'5': 'jkl',
260-
'6': 'mno',
261-
'7': 'pqrs',
262-
'8': 'tuv',
263-
'9': 'wxyz'
262+
private digitToLetters: { [key: string]: string } = {
263+
"2": "abc",
264+
"3": "def",
265+
"4": "ghi",
266+
"5": "jkl",
267+
"6": "mno",
268+
"7": "pqrs",
269+
"8": "tuv",
270+
"9": "wxyz",
271+
};
272+
273+
letterCombinations(digits: string): string[] {
274+
const combinations: string[] = [];
275+
276+
const backtrack = (index: number, path: string): void => {
277+
if (index === digits.length) {
278+
combinations.push(path);
279+
return;
280+
}
281+
const letters = this.digitToLetters[digits.charAt(index)];
282+
for (let letter of letters) {
283+
backtrack(index + 1, path + letter);
284+
}
264285
};
265286

266-
letterCombinations(digits: string): string[] {
267-
const combinations: string[] = [];
268-
269-
const backtrack = (index: number, path: string): void => {
270-
if (index === digits.length) {
271-
combinations.push(path);
272-
return;
273-
}
274-
const letters = this.digitToLetters[digits.charAt(index)];
275-
for (let letter of letters) {
276-
backtrack(index + 1, path + letter);
277-
}
278-
};
279-
280-
if (digits.length !== 0) {
281-
backtrack(0, '');
282-
}
283-
284-
return combinations;
287+
if (digits.length !== 0) {
288+
backtrack(0, "");
285289
}
290+
291+
return combinations;
292+
}
286293
}
287294

288295
// Example usage:
@@ -295,9 +302,11 @@ console.log(solution.letterCombinations("23")); // Output: ["ad","ae","af","bd",
295302
Here's a step-by-step algorithm for generating all possible letter combinations of a given string of digits using backtracking:
296303

297304
1. **Define a mapping of digits to letters:**
305+
298306
- Create a map where each digit from 2 to 9 is mapped to its corresponding letters on a telephone keypad.
299307

300308
2. **Define a backtracking function:**
309+
301310
- The function will take the following parameters:
302311
- `index`: The current index in the digits string.
303312
- `path`: The current combination of letters.
@@ -306,11 +315,12 @@ Here's a step-by-step algorithm for generating all possible letter combinations
306315
- After the recursive call, remove the last character from the combination (backtracking).
307316

308317
3. **Base Case:**
318+
309319
- If the length of the current combination is equal to the length of the input digits string, add the combination to the result list.
310320

311321
4. **Main Function:**
312322
- Initialize an empty list to store the combinations.
313323
- Call the backtracking function with the initial index set to 0 and an empty string as the initial combination.
314324
- Return the list of combinations.
315325

316-
This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking.
326+
This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking.

0 commit comments

Comments
 (0)