Skip to content

Commit b874f92

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

File tree

4 files changed

+49
-70
lines changed

4 files changed

+49
-70
lines changed

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

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -397,24 +397,17 @@ function letterCombinations(digits: string): string[] {
397397
}
398398
const ans: string[] = [];
399399
const n = digits.length;
400-
const map: Record<string, string[]> = {
401-
2: [...'abc'],
402-
3: [...'def'],
403-
4: [...'ghi'],
404-
5: [...'jkl'],
405-
6: [...'mno'],
406-
7: [...'pqrs'],
407-
8: [...'tuv'],
408-
9: [...'wxyz'],
409-
};
400+
const d: string[][] = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'].map(x => [
401+
...x,
402+
]);
410403

411404
const dfs = (curr: string, start: number) => {
412405
if (curr.length === n) {
413406
ans.push(curr);
414407
return;
415408
}
416409
for (let i = start; i < n; i++) {
417-
for (const ch of map[digits[i]]) {
410+
for (const ch of d[+digits[i] - 2]) {
418411
dfs(curr + ch, i + 1);
419412
}
420413
}
@@ -462,25 +455,25 @@ impl Solution {
462455
* @return {string[]}
463456
*/
464457
var letterCombinations = function (digits) {
465-
if (digits.length == 0) {
458+
if (digits.length === 0) {
466459
return [];
467460
}
468461
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(''));
462+
const n = digits.length;
463+
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'].map(x => [...x]);
464+
465+
const dfs = (curr, start) => {
466+
if (curr.length === n) {
467+
ans.push(curr);
474468
return;
475469
}
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();
470+
for (let i = start; i < n; i++) {
471+
for (const ch of map[+digits[i] - 2]) {
472+
dfs(curr + ch, i + 1);
473+
}
481474
}
482475
};
483-
dfs(0);
476+
dfs('', 0);
484477
return ans;
485478
};
486479
```

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

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -393,31 +393,24 @@ function letterCombinations(digits: string): string[] {
393393
}
394394
const ans: string[] = [];
395395
const n = digits.length;
396-
const map: Record<string, string[]> = {
397-
2: [...'abc'],
398-
3: [...'def'],
399-
4: [...'ghi'],
400-
5: [...'jkl'],
401-
6: [...'mno'],
402-
7: [...'pqrs'],
403-
8: [...'tuv'],
404-
9: [...'wxyz'],
405-
};
396+
const d: string[][] = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'].map(x => [
397+
...x,
398+
]);
399+
406400
const dfs = (curr: string, start: number) => {
407401
if (curr.length === n) {
408402
ans.push(curr);
409403
return;
410404
}
411405
for (let i = start; i < n; i++) {
412-
for (const ch of map[digits[i]]) {
406+
for (const ch of d[+digits[i] - 2]) {
413407
dfs(curr + ch, i + 1);
414408
}
415409
}
416410
};
417411
dfs('', 0);
418412
return ans;
419413
}
420-
421414
```
422415

423416
#### Rust
@@ -458,25 +451,25 @@ impl Solution {
458451
* @return {string[]}
459452
*/
460453
var letterCombinations = function (digits) {
461-
if (digits.length == 0) {
454+
if (digits.length === 0) {
462455
return [];
463456
}
464457
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(''));
458+
const n = digits.length;
459+
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'].map(x => [...x]);
460+
461+
const dfs = (curr, start) => {
462+
if (curr.length === n) {
463+
ans.push(curr);
470464
return;
471465
}
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();
466+
for (let i = start; i < n; i++) {
467+
for (const ch of map[+digits[i] - 2]) {
468+
dfs(curr + ch, i + 1);
469+
}
477470
}
478471
};
479-
dfs(0);
472+
dfs('', 0);
480473
return ans;
481474
};
482475
```

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
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 d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'].map(x => [...x]);
12+
13+
const dfs = (curr, start) => {
14+
if (curr.length === n) {
15+
ans.push(curr);
1516
return;
1617
}
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();
18+
for (let i = start; i < n; i++) {
19+
for (const ch of map[+digits[i] - 2]) {
20+
dfs(curr + ch, i + 1);
21+
}
2222
}
2323
};
24-
dfs(0);
24+
dfs('', 0);
2525
return ans;
2626
};

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,17 @@ function letterCombinations(digits: string): string[] {
44
}
55
const ans: string[] = [];
66
const n = digits.length;
7-
const map: Record<string, string[]> = {
8-
2: [...'abc'],
9-
3: [...'def'],
10-
4: [...'ghi'],
11-
5: [...'jkl'],
12-
6: [...'mno'],
13-
7: [...'pqrs'],
14-
8: [...'tuv'],
15-
9: [...'wxyz'],
16-
};
7+
const d: string[][] = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'].map(x => [
8+
...x,
9+
]);
1710

1811
const dfs = (curr: string, start: number) => {
1912
if (curr.length === n) {
2013
ans.push(curr);
2114
return;
2215
}
2316
for (let i = start; i < n; i++) {
24-
for (const ch of map[digits[i]]) {
17+
for (const ch of d[+digits[i] - 2]) {
2518
dfs(curr + ch, i + 1);
2619
}
2720
}

0 commit comments

Comments
 (0)