-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy path0017. Letter Combinations of a Phone Number.js
53 lines (47 loc) · 1.23 KB
/
0017. 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
// Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.
//
// Example:
//
// Input: "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.
/**
* @param {string} digits
* @return {string[]}
*/
// Backtracking
// Time O(3^N * 4^M)
// N is the number of digits in the input that maps to 3 letters (e.g. 2, 3, 4, 5, 6, 8)
// M is the number of digits in the input that maps to 4 letters (e.g. 7, 9)
//
// Space O(3^N * 4^M) since one has to keep O(3^N * 4^M) solutions.
const letterCombinations = (digits) => {
if (digits == null || digits.length === 0) return [];
const map = {
2: 'abc',
3: 'def',
4: 'ghi',
5: 'jkl',
6: 'mno',
7: 'pqrs',
8: 'tuv',
9: 'wxyz',
};
const res = [];
const go = (i, s) => {
if (i === digits.length) {
res.push(s);
return;
}
for (const c of map[digits[i]]) {
go(i + 1, s + c);
}
};
go(0, '');
return res;
};