Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e33d27b

Browse filesBrowse files
authored
Merge pull request Tahirc1#11 from Tahirc1/Tahirc1-update-17
Update 17. Letter Combinations of a Phone Number.js
2 parents 08a7322 + d81656a commit e33d27b
Copy full SHA for e33d27b

File tree

Expand file treeCollapse file tree

1 file changed

+16
-1
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+16
-1
lines changed
+16-1Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.