Skip to content

Commit 374bc21

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

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
@@ -392,25 +392,34 @@ func letterCombinations(digits string) (ans []string) {
392392

393393
```ts
394394
function letterCombinations(digits: string): string[] {
395-
if (digits.length == 0) {
395+
if (digits.length === 0) {
396396
return [];
397397
}
398398
const ans: string[] = [];
399-
const t: string[] = [];
400-
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
401-
const dfs = (i: number) => {
402-
if (i >= digits.length) {
403-
ans.push(t.join(''));
399+
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+
};
410+
411+
const dfs = (curr: string, start: number) => {
412+
if (curr.length === n) {
413+
ans.push(curr);
404414
return;
405415
}
406-
const s = d[parseInt(digits[i]) - 2];
407-
for (const c of s) {
408-
t.push(c);
409-
dfs(i + 1);
410-
t.pop();
416+
for (let i = start; i < n; i++) {
417+
for (const ch of map[digits[i]]) {
418+
dfs(curr + ch, i + 1);
419+
}
411420
}
412421
};
413-
dfs(0);
422+
dfs('', 0);
414423
return ans;
415424
}
416425
```

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
@@ -388,27 +388,36 @@ func letterCombinations(digits string) (ans []string) {
388388

389389
```ts
390390
function letterCombinations(digits: string): string[] {
391-
if (digits.length == 0) {
391+
if (digits.length === 0) {
392392
return [];
393393
}
394394
const ans: string[] = [];
395-
const t: string[] = [];
396-
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
397-
const dfs = (i: number) => {
398-
if (i >= digits.length) {
399-
ans.push(t.join(''));
395+
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+
};
406+
const dfs = (curr: string, start: number) => {
407+
if (curr.length === n) {
408+
ans.push(curr);
400409
return;
401410
}
402-
const s = d[parseInt(digits[i]) - 2];
403-
for (const c of s) {
404-
t.push(c);
405-
dfs(i + 1);
406-
t.pop();
411+
for (let i = start; i < n; i++) {
412+
for (const ch of map[digits[i]]) {
413+
dfs(curr + ch, i + 1);
414+
}
407415
}
408416
};
409-
dfs(0);
417+
dfs('', 0);
410418
return ans;
411419
}
420+
412421
```
413422

414423
#### Rust
Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
function letterCombinations(digits: string): string[] {
2-
if (digits.length == 0) {
2+
if (digits.length === 0) {
33
return [];
44
}
55
const ans: string[] = [];
6-
const t: string[] = [];
7-
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
8-
const dfs = (i: number) => {
9-
if (i >= digits.length) {
10-
ans.push(t.join(''));
6+
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+
};
17+
18+
const dfs = (curr: string, start: number) => {
19+
if (curr.length === n) {
20+
ans.push(curr);
1121
return;
1222
}
13-
const s = d[parseInt(digits[i]) - 2];
14-
for (const c of s) {
15-
t.push(c);
16-
dfs(i + 1);
17-
t.pop();
23+
for (let i = start; i < n; i++) {
24+
for (const ch of map[digits[i]]) {
25+
dfs(curr + ch, i + 1);
26+
}
1827
}
1928
};
20-
dfs(0);
29+
dfs('', 0);
2130
return ans;
2231
}

0 commit comments

Comments
 (0)