Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitd81656a

Browse files
authored
Update 17. Letter Combinations of a Phone Number.js
1 parent08a7322 commitd81656a

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
varletterCombinations=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
letk=[0,0,"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"]
7+
// we convert first digit to number
48
letm=Number(digits[0])
9+
// we split the corresponding string to alphabets
10+
// and it will store all output
511
leta=k[m].split("")
612
let[p,n]=[[],[]]
13+
// we loop over all digits from index 1 to last index
714
for(leti=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(letj=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
returna
16-
};
31+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp