-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.keypad.js
More file actions
40 lines (34 loc) · 986 Bytes
/
string.keypad.js
File metadata and controls
40 lines (34 loc) · 986 Bytes
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
/*
* @title: Keypad combinations
* @description: find all number keypad combinations of input
* @author: Thorsten Kober
* @email: [email protected]
*/
const chars = ['', '', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
function createKeypadWords(arr) {
const result = [];
function create(arr, index, output) {
if (index === arr.length) {
result.push(output.join(''));
return;
}
const key = chars[arr[index]];
for (let i = 0; i < key.length; i++) {
output[index] = key[i];
create(arr, index + 1, output);
}
}
create(arr, 0, []);
return result;
}
// npx jest algorithms/string/string.keypad.js
test('createKeypadWords', () => {
expect(createKeypadWords([2, 3, 4], 3)).toEqual([
'adg', 'adh', 'adi', 'aeg',
'aeh', 'aei', 'afg', 'afh',
'afi', 'bdg', 'bdh', 'bdi',
'beg', 'beh', 'bei', 'bfg',
'bfh', 'bfi', 'cdg', 'cdh',
'cdi', 'ceg', 'ceh', 'cei',
'cfg', 'cfh', 'cfi']);
});