Skip to content

Commit ec7d9b2

Browse files
committed
feat: update js solution to lc problem: No.0017
1 parent 374bc21 commit ec7d9b2

File tree

3 files changed

+63
-36
lines changed

3 files changed

+63
-36
lines changed

solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -462,25 +462,34 @@ impl Solution {
462462
* @return {string[]}
463463
*/
464464
var letterCombinations = function (digits) {
465-
if (digits.length == 0) {
465+
if (digits.length === 0) {
466466
return [];
467467
}
468468
const ans = [];
469-
const t = [];
470-
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
471-
const dfs = i => {
472-
if (i >= digits.length) {
473-
ans.push(t.join(''));
469+
const n = digits.length;
470+
const map = {
471+
2: [...'abc'],
472+
3: [...'def'],
473+
4: [...'ghi'],
474+
5: [...'jkl'],
475+
6: [...'mno'],
476+
7: [...'pqrs'],
477+
8: [...'tuv'],
478+
9: [...'wxyz'],
479+
};
480+
481+
const dfs = (curr, start) => {
482+
if (curr.length === n) {
483+
ans.push(curr);
474484
return;
475485
}
476-
const s = d[parseInt(digits[i]) - 2];
477-
for (const c of s) {
478-
t.push(c);
479-
dfs(i + 1);
480-
t.pop();
486+
for (let i = start; i < n; i++) {
487+
for (const ch of map[digits[i]]) {
488+
dfs(curr + ch, i + 1);
489+
}
481490
}
482491
};
483-
dfs(0);
492+
dfs('', 0);
484493
return ans;
485494
};
486495
```

solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -458,25 +458,34 @@ impl Solution {
458458
* @return {string[]}
459459
*/
460460
var letterCombinations = function (digits) {
461-
if (digits.length == 0) {
461+
if (digits.length === 0) {
462462
return [];
463463
}
464464
const ans = [];
465-
const t = [];
466-
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
467-
const dfs = i => {
468-
if (i >= digits.length) {
469-
ans.push(t.join(''));
465+
const n = digits.length;
466+
const map = {
467+
2: [...'abc'],
468+
3: [...'def'],
469+
4: [...'ghi'],
470+
5: [...'jkl'],
471+
6: [...'mno'],
472+
7: [...'pqrs'],
473+
8: [...'tuv'],
474+
9: [...'wxyz'],
475+
};
476+
477+
const dfs = (curr, start) => {
478+
if (curr.length === n) {
479+
ans.push(curr);
470480
return;
471481
}
472-
const s = d[parseInt(digits[i]) - 2];
473-
for (const c of s) {
474-
t.push(c);
475-
dfs(i + 1);
476-
t.pop();
482+
for (let i = start; i < n; i++) {
483+
for (const ch of map[digits[i]]) {
484+
dfs(curr + ch, i + 1);
485+
}
477486
}
478487
};
479-
dfs(0);
488+
dfs('', 0);
480489
return ans;
481490
};
482491
```

solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,33 @@
33
* @return {string[]}
44
*/
55
var letterCombinations = function (digits) {
6-
if (digits.length == 0) {
6+
if (digits.length === 0) {
77
return [];
88
}
99
const ans = [];
10-
const t = [];
11-
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
12-
const dfs = i => {
13-
if (i >= digits.length) {
14-
ans.push(t.join(''));
10+
const n = digits.length;
11+
const map = {
12+
2: [...'abc'],
13+
3: [...'def'],
14+
4: [...'ghi'],
15+
5: [...'jkl'],
16+
6: [...'mno'],
17+
7: [...'pqrs'],
18+
8: [...'tuv'],
19+
9: [...'wxyz'],
20+
};
21+
22+
const dfs = (curr, start) => {
23+
if (curr.length === n) {
24+
ans.push(curr);
1525
return;
1626
}
17-
const s = d[parseInt(digits[i]) - 2];
18-
for (const c of s) {
19-
t.push(c);
20-
dfs(i + 1);
21-
t.pop();
27+
for (let i = start; i < n; i++) {
28+
for (const ch of map[digits[i]]) {
29+
dfs(curr + ch, i + 1);
30+
}
2231
}
2332
};
24-
dfs(0);
33+
dfs('', 0);
2534
return ans;
2635
};

0 commit comments

Comments
 (0)