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

Commit9447151

Browse files
author
sambabib
committed
js solution to _17
1 parent30ac3db commit9447151

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

‎javascript/_17.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
functionletterCombinations(digits){
2+
// If the input is an empty string, return an empty array.
3+
if(digits.length===0){
4+
return[];
5+
}
6+
7+
// Mapping of digits to letters as per the telephone keypad using a javascript dictionary.
8+
constdigitToChar={
9+
'2':['a','b','c'],
10+
'3':['d','e','f'],
11+
'4':['g','h','i'],
12+
'5':['j','k','l'],
13+
'6':['m','n','o'],
14+
'7':['p','q','r','s'],
15+
'8':['t','u','v'],
16+
'9':['w','x','y','z']
17+
};
18+
19+
// Resultant array to store all possible combinations
20+
constresult=[];
21+
22+
// Backtracking function to generate combinations
23+
functionbacktrack(index,currentCombination){
24+
// if the current combination has the same length as the input digits.
25+
if(index===digits.length){
26+
result.push(currentCombination);
27+
return;
28+
}
29+
30+
// Get the letters that the current digit maps to.
31+
letletters=digitToChar[digits[index]];
32+
33+
// Loop through the letters and call backtrack recursively for the next digit.
34+
for(letletterofletters){
35+
backtrack(index+1,currentCombination+letter);
36+
}
37+
}
38+
39+
// Start backtracking from the first digit (index 0) with an empty string as the initial combination.
40+
backtrack(0,'');
41+
42+
returnresult;
43+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp