-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path17. Letter Combinations of a Phone Number.js
62 lines (54 loc) · 1.24 KB
/
17. Letter Combinations of a Phone Number.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
1 2 3
abc def
4 5 6
ghi jkl mno
7 8 9
pqrs tuv wxyz
0
*/
/**
* Leetcode Fundamental: 11/14 Update
* Failure:
* 1. Fail to find recursive case: pass substring digits.slice(1)
* 2. Fail to decide base case: digits.length === 0
* 3. Fail to make a coopy of candidate for each recursive call
* Since we need to make a copy, no need to use array, string is ok
*
* T: O(4^n)
* Runtime: 52 ms
*/
/**
* @param {string} digits
* @return {string[]}
*/
var letterCombinations = function(digits) {
// Handle edge case
if (digits === undefined || digits.length === 0) return [];
let map = {
2: "abc",
3: "def",
4: "ghi",
5: "jkl",
6: "mno",
7: "pqrs",
8: "tuv",
9: "wxyz"
}
let result = [];
const dfs = (digits, candidate) => {
// Base case
if (digits.length === 0) {
result.push(candidate);
return;
}
// Recursive case
for (let char of map[digits[0]]) {
dfs(digits.slice(1), candidate + char);
}
};
dfs(digits, "");
return result;
};