-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathletterCombinationsOfAPhoneNumber.js
126 lines (102 loc) · 3.14 KB
/
letterCombinationsOfAPhoneNumber.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const letterCombinations = digits => {
if (digits === '') return [];
const hash = {
2: ['a', 'b' ,'c'],
3: ['d', 'e', 'f'],
4: ['g', 'h', 'i'],
5: ['j', 'k', 'l'],
6: ['m', 'n', 'o'],
7: ['p', 'q', 'r', 's'],
8: ['t', 'u', 'v'],
9: ['w', 'x', 'y', 'z']
};
if (hash[digits]) return hash[digits];
const createCombinations = (str, prev, index) => {
if (index === str.length) return;
const prevArr= hash[prev];
const currNum = str[index];
const currArr = hash[currNum];
const comboStr = prev + str[index];
const output = [];
for (let i = 0; i < prevArr.length; i++) {
for (let j = 0; j < currArr.length; j++) {
output.push(prevArr[i] + currArr[j]);
}
}
hash[comboStr] = output;
createCombinations(str, comboStr, index + 1);
};
createCombinations(digits, digits[0], 1);
return hash[digits];
};
// const letterCombinations = digits => {
// if (digits === '') return [];
// const hash = {
// 2: ['a', 'b' ,'c'],
// 3: ['d', 'e', 'f'],
// 4: ['g', 'h', 'i'],
// 5: ['j', 'k', 'l'],
// 6: ['m', 'n', 'o'],
// 7: ['p', 'q', 'r', 's'],
// 8: ['t', 'u', 'v'],
// 9: ['w', 'x', 'y', 'z']
// };
// if (hash[digits]) return hash[digits];
// const createCombinations = (prev, curr) => {
// if (curr === '') return;
// const prevArr= hash[prev];
// const currNum = curr[0];
// const currArr = hash[currNum];
// const comboStr = prev + curr[0];
// const output = [];
// for (let i = 0; i < prevArr.length; i++) {
// for (let j = 0; j < currArr.length; j++) {
// output.push(prevArr[i] + currArr[j]);
// }
// }
// hash[comboStr] = output;
// createCombinations(comboStr, curr.slice(1));
// };
// createCombinations(digits[0], digits.slice(1))
// return hash[digits];
// };
// const letterCombinations = function(digits) {
// if (digits == '') return [];
// const results = [];
// findLetterCombinations(digits, 0, '', results);
// return results;
// };
// const findLetterCombinations = function(digits, startIndex, prefix, results) {
// if (startIndex == digits.length) {
// results.push(prefix);
// return;
// }
// const map = {
// 2: 'abc', 3: 'def', 4: 'ghi', 5: 'jkl',
// 6: 'mno', 7: 'pqrs', 8: 'tuv', 9: 'wxyz'
// };
// const curr = map[digits[startIndex]];
// for (let i = 0; i < curr.length; i++) {
// const letter = curr[i];
// prefix += letter;
// findLetterCombinations(digits, startIndex + 1, prefix, results);
// prefix = prefix.substring(0, prefix.length - 1);
// }
// };
/*
specification
input: string containing digits from two to nine inclusive
output: array of all possible letter combinations that the digits could represent
constraints:
edge cases:
justification
to find all the combinations a number could represent
explanation
the input digits will determine the output combinations
visualization
approximation
create a hash to store each number and the letters it corresponds to
verification
implementation
*/
console.log(letterCombinations('23')); // ['ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf']