forked from chihungyu1116/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
17 Letter Combinations of a Phone Number.js
57 lines (43 loc) · 1.26 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
// 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.
// Input:Digit string "23"
// Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
// Note:
// Although the above answer is in lexicographical order, your answer could be in any order you want.
// Hide Company Tags Amazon Dropbox Google Uber Facebook
// Hide Tags Backtracking String
// Hide Similar Problems (M) Generate Parentheses (M) Combination Sum
/**
* @param {string} digits
* @return {string[]}
*/
var numToLetters = {
'0': ' ',
'1': '',
'2': 'abc',
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'pqrs',
'8': 'tuv',
'9': 'wxyz'
};
var letterCombinations = function(digits) {
var res = [];
if(digits.length === 0) {
return res;
}
function dfs(digits, idx, curr) {
if(idx === digits.length) {
res.push(curr);
return;
}
var letters = numToLetters[digits[idx]];
for(var i = 0; i < letters.length; i++) {
dfs(digits, idx + 1, curr + letters[i]);
}
}
dfs(digits, 0, '');
return res;
};