-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathletter Combinations.js
52 lines (46 loc) · 1.01 KB
/
letter Combinations.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
// /**
// * @param {number[]} matchsticks
// * @return {boolean}
// */
// var makesquare = function(matchsticks) {
// const obj = {};
// matchsticks.forEach(e => {
// obj[e] = (obj[e]||0)+1
// })
// let sum = 0;
// matchsticks.forEach(e => {
// sum = sum+e
// })
// return sum%2===0
// };
// console.log(makesquare([1,1,2,2,2]))
/**
* @param {string} digits
* @return {string[]}
*/
var letterCombinations = function(digits) {
const obj = {
2: 'abc',
3: 'def',
4: 'ghi',
5: 'jkl',
6: 'mno',
7: 'pqrs',
8: 'tuv',
9: 'wxyz',
}
const res = [];
const dfs = (i, slate) => {
if(i === digits.length) {
res.push(slate.join(''))
return;
}
const currentArr = obj[digits[i]];
for(let j=0; j<currentArr.length; j++) {
dfs(i+1, slate.concat(currentArr[j]))
}
}
dfs(0, [])
return res
};
console.log(letterCombinations('23'))