Skip to content

Commit 621f327

Browse files
committed
feat: add ts solution to lc problem: No.0017
No.0017.Letter Combinations of a Phone Number
1 parent b7121be commit 621f327

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,39 @@ function letterCombinations(digits: string): string[] {
220220
}
221221
```
222222

223+
```ts
224+
const map = {
225+
'2': ['a', 'b', 'c'],
226+
'3': ['d', 'e', 'f'],
227+
'4': ['g', 'h', 'i'],
228+
'5': ['j', 'k', 'l'],
229+
'6': ['m', 'n', 'o'],
230+
'7': ['p', 'q', 'r', 's'],
231+
'8': ['t', 'u', 'v'],
232+
'9': ['w', 'x', 'y', 'z']
233+
}
234+
235+
function letterCombinations(digits: string): string[] {
236+
const n = digits.length
237+
if (n === 0) {
238+
return []
239+
}
240+
const dfs = (i: number, ss: string[]) => {
241+
if (i === n) {
242+
return ss
243+
}
244+
const t = []
245+
for (const c of map[digits[i]]) {
246+
for (const s of ss) {
247+
t.push(s + c)
248+
}
249+
}
250+
return dfs(i + 1, t)
251+
}
252+
return dfs(1, map[digits[0]])
253+
};
254+
```
255+
223256
### **Rust**
224257

225258
```rust

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,39 @@ function letterCombinations(digits: string): string[] {
210210
}
211211
```
212212

213+
```ts
214+
const map = {
215+
'2': ['a', 'b', 'c'],
216+
'3': ['d', 'e', 'f'],
217+
'4': ['g', 'h', 'i'],
218+
'5': ['j', 'k', 'l'],
219+
'6': ['m', 'n', 'o'],
220+
'7': ['p', 'q', 'r', 's'],
221+
'8': ['t', 'u', 'v'],
222+
'9': ['w', 'x', 'y', 'z']
223+
}
224+
225+
function letterCombinations(digits: string): string[] {
226+
const n = digits.length
227+
if (n === 0) {
228+
return []
229+
}
230+
const dfs = (i: number, ss: string[]) => {
231+
if (i === n) {
232+
return ss
233+
}
234+
const t = []
235+
for (const c of map[digits[i]]) {
236+
for (const s of ss) {
237+
t.push(s + c)
238+
}
239+
}
240+
return dfs(i + 1, t)
241+
}
242+
return dfs(1, map[digits[0]])
243+
};
244+
```
245+
213246
### **Rust**
214247

215248
```rust

0 commit comments

Comments
 (0)