8000 Update 17. Letter Combinations of a Phone Number.js · Tahirc1/LeetcodeJs@d81656a · GitHub
[go: up one dir, main page]

Skip to content

Commit d81656a

Browse files
authored
Update 17. Letter Combinations of a Phone Number.js
1 parent 08a7322 commit d81656a

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
var letterCombinations = function(digits) {
2+
// if digits is empty return []
23
if(digits == ""){return []}
4+
// we create all possible strings a digit might correspond to
5+
// digit contains number 2-9 so we keep index 0 and 1 as 0
36
let k = [0,0,"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"]
7+
// we convert first digit to number
48
let m = Number(digits[0])
9+
// we split the corresponding string to alphabets
10+
// and it will store all output
511
let a = k[m].split("")
612
let [p,n] = [[],[]]
13+
// we loop over all digits from index 1 to last index
714
for(let i = 1 ; i < digits.length ;i++){
15+
// p will store all possible alphabets for current digit
816
p = k[digits[i]].split("")
17+
// n will store all the possible combinations made for that digit
918
n = []
19+
// all possible combinations for given digit are are p.length*a.length
1020
for(let j = 0 ; j < p.length*a.length ;j++){
21+
// Math.floor(j/p.length) this let us stay in range of index 0-a.length-1
22+
// j%p.length this let us stay in range of index 0-p.length-1
23+
// thus going over all possible combination for all given a and p indices
1124
n.push(a[Math.floor(j/p.length)]+p[j%p.length])
1225
}
26+
// we update the array a will all current possiblities
1327
a = n
1428
}
29+
// we return all possible combinations
1530
return a
16-
};
31+
};

0 commit comments

Comments
 (0)
0