Skip to content

Commit 13c4b85

Browse files
committed
updated changes
1 parent 60b2935 commit 13c4b85

File tree

1 file changed

+67
-77
lines changed

1 file changed

+67
-77
lines changed

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

Lines changed: 67 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
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-
2221
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.
2322

2423
### Examples
@@ -33,13 +32,13 @@ Given a string containing digits from 2-9 inclusive, return all possible letter
3332
- **Input:** `digits = ""`
3433
- **Output:** `[]`
3534

35+
3636
#### Example 3
3737

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

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

5049
1. **Mapping Digits to Letters:**
51-
5250
- Define a mapping of digits to their corresponding letters, similar to telephone buttons.
5351

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

6461
3. **Base Case:**
65-
6662
- 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.
6763

6864
4. **Main Function:**
@@ -158,7 +154,6 @@ public class Solution {
158154
```
159155

160156
#### CPP:
161-
162157
```cpp
163158
#include <iostream>
164159
#include <vector>
@@ -214,82 +209,80 @@ int main() {
214209
```
215210
216211
#### JavaScript
217-
218212
```js
219213
/**
220214
* @param {string} digits
221215
* @return {string[]}
222216
*/
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;
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;
252246
};
253247
254248
// Example usage:
255249
console.log(letterCombinations("23")); // Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
256250
```
257251

258252
#### TypeScript
259-
260253
```ts
261254
class Solution {
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-
}
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'
285264
};
286265

287-
if (digits.length !== 0) {
288-
backtrack(0, "");
289-
}
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+
};
290279

291-
return combinations;
292-
}
280+
if (digits.length !== 0) {
281+
backtrack(0, '');
282+
}
283+
284+
return combinations;
285+
}
293286
}
294287

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

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

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

317308
3. **Base Case:**
318-
319309
- If the length of the current combination is equal to the length of the input digits string, add the combination to the result list.
320310

321311
4. **Main Function:**
322312
- Initialize an empty list to store the combinations.
323313
- Call the backtracking function with the initial index set to 0 and an empty string as the initial combination.
324314
- Return the list of combinations.
325315

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

0 commit comments

Comments
 (0)