forked from hitesh00025/Algorithms-In-Javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphoneNumberDigitsKeyPress.js
More file actions
85 lines (53 loc) · 1.19 KB
/
Copy pathphoneNumberDigitsKeyPress.js
File metadata and controls
85 lines (53 loc) · 1.19 KB
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
/**
The goal of this assignment is to convert key pressed to all possible outcomes.
* @param {string} digits
* @return {string[]}
*/
var letterCombinations = function(digits) {
//TODO figure out the solution for more than 2 keys pressed.
var result = [];
var first = digitToChar(digits[0]);
var second = digitToChar(digits[1]);
if (digits.indexOf('0') > -1 || digits.length === 0) {
return [];
}
if (digits.length === 1) {
for (var i = 0; i < first.length; i++) {
result.push([first[i]]);
}
} else {
for (var i = 0; i < first.length; i++) {
for (var j = 0; j < second.length; j++) {
result.push(first[i] + second[j]);
}
}
}
return result;
};
function digitToChar(str) {
if (str === '2') {
return "abc";
}
if (str === '3') {
return "def";
}
if (str === '4') {
return "ghi";
}
if (str === '5') {
return "jkl";
}
if (str === '6') {
return "mno";
}
if (str === '7') {
return "pqrs";
}
if (str === '8') {
return "tuv";
}
if (str === '9') {
return "wxyz";
}
}
console.log(letterCombinations('23'));//[ 'ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf' ]