Skip to content

Commit d6d04a1

Browse files
committed
Updated solution for 0017-Letter-Combinations-of-a-Phone-Number
1 parent fc02358 commit d6d04a1

File tree

1 file changed

+65
-75
lines changed

1 file changed

+65
-75
lines changed

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

Lines changed: 65 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,21 @@ id: letter-combinations-of-a-phone-number
33
title: Letter Combinations of a Phone Number (LeetCode)
44
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.
10-
sidebar_position: 17
1110
---
1211

1312
## Problem Description
1413

15-
| Problem Statement | Solution Link | LeetCode Profile |
16-
| :------------------------------------------------------------------------------------------------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :----------------------------------------------------- |
17-
| [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/) |
1817

1918
### Problem Description
2019

2120
## Problem Statement:
22-
2321
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.
2422

2523
### Examples
@@ -34,6 +32,7 @@ Given a string containing digits from 2-9 inclusive, return all possible letter
3432
- **Input:** `digits = ""`
3533
- **Output:** `[]`
3634

35+
3736
#### Example 3
3837

3938
- **Input:** `2`
@@ -49,11 +48,9 @@ Given a string containing digits from 2-9 inclusive, return all possible letter
4948
### Approach
5049

5150
1. **Mapping Digits to Letters:**
52-
5351
- Define a mapping of digits to their corresponding letters, similar to telephone buttons.
5452

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

6562
3. **Base Case:**
66-
6763
- 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.
6864

6965
4. **Main Function:**
@@ -159,7 +155,6 @@ public class Solution {
159155
```
160156

161157
#### CPP:
162-
163158
```cpp
164159
#include <iostream>
165160
#include <vector>
@@ -215,82 +210,80 @@ int main() {
215210
```
216211
217212
#### JavaScript
218-
219213
```js
220214
/**
221215
* @param {string} digits
222216
* @return {string[]}
223217
*/
224-
var letterCombinations = function (digits) {
225-
if (digits.length === 0) return [];
226-
227-
const digitToLetters = {
228-
2: "abc",
229-
3: "def",
230-
4: "ghi",
231-
5: "jkl",
232-
6: "mno",
233-
7: "pqrs",
234-
8: "tuv",
235-
9: "wxyz",
236-
};
237-
238-
const combinations = [];
239-
240-
const backtrack = (index, path) => {
241-
if (index === digits.length) {
242-
combinations.push(path);
243-
return;
244-
}
245-
const letters = digitToLetters[digits.charAt(index)];
246-
for (let letter of letters) {
247-
backtrack(index + 1, path + letter);
248-
}
249-
};
250-
251-
backtrack(0, "");
252-
return combinations;
218+
var letterCombinations = function(digits) {
219+
if (digits.length === 0) return [];
220+
221+
const digitToLetters = {
222+
'2': 'abc',
223+
'3': 'def',
224+
'4': 'ghi',
225+
'5': 'jkl',
226+
'6': 'mno',
227+
'7': 'pqrs',
228+
'8': 'tuv',
229+
'9': 'wxyz'
230+
};
231+
232+
const combinations = [];
233+
234+
const backtrack = (index, path) => {
235+
if (index === digits.length) {
236+
combinations.push(path);
237+
return;
238+
}
239+
const letters = digitToLetters[digits.charAt(index)];
240+
for (let letter of letters) {
241+
backtrack(index + 1, path + letter);
242+
}
243+
};
244+
245+
backtrack(0, '');
246+
return combinations;
253247
};
254248
255249
// Example usage:
256250
console.log(letterCombinations("23")); // Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
257251
```
258252

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

288-
if (digits.length !== 0) {
289-
backtrack(0, "");
290-
}
267+
letterCombinations(digits: string): string[] {
268+
const combinations: string[] = [];
269+
270+
const backtrack = (index: number, path: string): void => {
271+
if (index === digits.length) {
272+
combinations.push(path);
273+
return;
274+
}
275+
const letters = this.digitToLetters[digits.charAt(index)];
276+
for (let letter of letters) {
277+
backtrack(index + 1, path + letter);
278+
}
279+
};
291280

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)