-
Notifications
You must be signed in to change notification settings - Fork 0
/
letterCombinationsOfaPhoneNumber.js
36 lines (34 loc) · 1.08 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
function letterComb(adjList, str, strItr, currentStr, outputArr){
if(strItr >= str.length) return;
if(strItr == str.length -1) {
let digit = str[strItr];
for(let itr = 0 ; itr < adjList[digit].length ; itr++) {
outputArr.push(
currentStr + adjList[digit][itr]
);
}
return;
}
let digit = str[strItr];
for(let itr = 0 ; itr < adjList[digit].length ; itr++) {
letterComb(adjList, str, strItr + 1, currentStr + adjList[digit][itr] , outputArr);
}
return;
}
var letterCombinations = function(digits) {
let adjList = {
'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'],
};
let outputArr = [];
if(!digits.length) return outputArr;
let currentStr = "";
letterComb(adjList, digits, 0, currentStr ,outputArr);
return outputArr;
};