Skip to content

Commit b7121be

Browse files
committed
feat: add solutions to lc problem: No.0017
No.0017.Letter Combinations of a Phone Number
1 parent 82220ba commit b7121be

File tree

4 files changed

+248
-0
lines changed

4 files changed

+248
-0
lines changed

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,92 @@ public class Solution {
186186
}
187187
```
188188

189+
### **TypeScript**
190+
191+
```ts
192+
const map = {
193+
'2': ['a', 'b', 'c'],
194+
'3': ['d', 'e', 'f'],
195+
'4': ['g', 'h', 'i'],
196+
'5': ['j', 'k', 'l'],
197+
'6': ['m', 'n', 'o'],
198+
'7': ['p', 'q', 'r', 's'],
199+
'8': ['t', 'u', 'v'],
200+
'9': ['w', 'x', 'y', 'z'],
201+
};
202+
203+
function letterCombinations(digits: string): string[] {
204+
const n = digits.length;
205+
if (n === 0) {
206+
return [];
207+
}
208+
const res = [];
209+
const dfs = (i: number, str: string) => {
210+
if (i === n) {
211+
res.push(str);
212+
return;
213+
}
214+
for (const c of map[digits[i]]) {
215+
dfs(i + 1, str + c);
216+
}
217+
};
218+
dfs(0, '');
219+
return res;
220+
}
221+
```
222+
223+
### **Rust**
224+
225+
```rust
226+
use std::collections::HashMap;
227+
228+
impl Solution {
229+
fn dfs(
230+
i: usize,
231+
s: &mut String,
232+
cs: &Vec<char>,
233+
map: &HashMap<char, String>,
234+
res: &mut Vec<String>,
235+
) {
236+
if i == cs.len() {
237+
res.push(s.clone());
238+
return;
239+
}
240+
for c in map.get(&cs[i]).unwrap().chars() {
241+
s.push(c);
242+
Self::dfs(i + 1, s, cs, map, res);
243+
s.pop();
244+
}
245+
}
246+
247+
pub fn letter_combinations(digits: String) -> Vec<String> {
248+
let mut res = vec![];
249+
if digits.is_empty() {
250+
return res;
251+
}
252+
253+
let mut map = HashMap::new();
254+
map.insert('2', String::from("abc"));
255+
map.insert('3', String::from("def"));
256+
map.insert('4', String::from("ghi"));
257+
map.insert('5', String::from("jkl"));
258+
map.insert('6', String::from("mno"));
259+
map.insert('7', String::from("pqrs"));
260+
map.insert('8', String::from("tuv"));
261+
map.insert('9', String::from("wxyz"));
262+
263+
Self::dfs(
264+
0,
265+
&mut String::new(),
266+
&digits.chars().collect(),
267+
&map,
268+
&mut res,
269+
);
270+
res
271+
}
272+
}
273+
```
274+
189275
### **...**
190276

191277
```

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,92 @@ public class Solution {
176176
}
177177
```
178178

179+
### **TypeScript**
180+
181+
```ts
182+
const map = {
183+
'2': ['a', 'b', 'c'],
184+
'3': ['d', 'e', 'f'],
185+
'4': ['g', 'h', 'i'],
186+
'5': ['j', 'k', 'l'],
187+
'6': ['m', 'n', 'o'],
188+
'7': ['p', 'q', 'r', 's'],
189+
'8': ['t', 'u', 'v'],
190+
'9': ['w', 'x', 'y', 'z'],
191+
};
192+
193+
function letterCombinations(digits: string): string[] {
194+
const n = digits.length;
195+
if (n === 0) {
196+
return [];
197+
}
198+
const res = [];
199+
const dfs = (i: number, str: string) => {
200+
if (i === n) {
201+
res.push(str);
202+
return;
203+
}
204+
for (const c of map[digits[i]]) {
205+
dfs(i + 1, str + c);
206+
}
207+
};
208+
dfs(0, '');
209+
return res;
210+
}
211+
```
212+
213+
### **Rust**
214+
215+
```rust
216+
use std::collections::HashMap;
217+
218+
impl Solution {
219+
fn dfs(
220+
i: usize,
221+
s: &mut String,
222+
cs: &Vec<char>,
223+
map: &HashMap<char, String>,
224+
res: &mut Vec<String>,
225+
) {
226+
if i == cs.len() {
227+
res.push(s.clone());
228+
return;
229+
}
230+
for c in map.get(&cs[i]).unwrap().chars() {
231+
s.push(c);
232+
Self::dfs(i + 1, s, cs, map, res);
233+
s.pop();
234+
}
235+
}
236+
237+
pub fn letter_combinations(digits: String) -> Vec<String> {
238+
let mut res = vec![];
239+
if digits.is_empty() {
240+
return res;
241+
}
242+
243+
let mut map = HashMap::new();
244+
map.insert('2', String::from("abc"));
245+
map.insert('3', String::from("def"));
246+
map.insert('4', String::from("ghi"));
247+
map.insert('5', String::from("jkl"));
248+
map.insert('6', String::from("mno"));
249+
map.insert('7', String::from("pqrs"));
250+
map.insert('8', String::from("tuv"));
251+
map.insert('9', String::from("wxyz"));
252+
253+
Self::dfs(
254+
0,
255+
&mut String::new(),
256+
&digits.chars().collect(),
257+
&map,
258+
&mut res,
259+
);
260+
res
261+
}
262+
}
263+
```
264+
179265
### **...**
180266

181267
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
fn dfs(
5+
i: usize,
6+
s: &mut String,
7+
cs: &Vec<char>,
8+
map: &HashMap<char, String>,
9+
res: &mut Vec<String>,
10+
) {
11+
if i == cs.len() {
12+
res.push(s.clone());
13+
return;
14+
}
15+
for c in map.get(&cs[i]).unwrap().chars() {
16+
s.push(c);
17+
Self::dfs(i + 1, s, cs, map, res);
18+
s.pop();
19+
}
20+
}
21+
22+
pub fn letter_combinations(digits: String) -> Vec<String> {
23+
let mut res = vec![];
24+
if digits.is_empty() {
25+
return res;
26+
}
27+
28+
let mut map = HashMap::new();
29+
map.insert('2', String::from("abc"));
30+
map.insert('3', String::from("def"));
31+
map.insert('4', String::from("ghi"));
32+
map.insert('5', String::from("jkl"));
33+
map.insert('6', String::from("mno"));
34+
map.insert('7', String::from("pqrs"));
35+
map.insert('8', String::from("tuv"));
36+
map.insert('9', String::from("wxyz"));
37+
38+
Self::dfs(
39+
0,
40+
&mut String::new(),
41+
&digits.chars().collect(),
42+
&map,
43+
&mut res,
44+
);
45+
res
46+
}
47+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const map = {
2+
'2': ['a', 'b', 'c'],
3+
'3': ['d', 'e', 'f'],
4+
'4': ['g', 'h', 'i'],
5+
'5': ['j', 'k', 'l'],
6+
'6': ['m', 'n', 'o'],
7+
'7': ['p', 'q', 'r', 's'],
8+
'8': ['t', 'u', 'v'],
9+
'9': ['w', 'x', 'y', 'z'],
10+
};
11+
12+
function letterCombinations(digits: string): string[] {
13+
const n = digits.length;
14+
if (n === 0) {
15+
return [];
16+
}
17+
const res = [];
18+
const dfs = (i: number, str: string) => {
19+
if (i === n) {
20+
res.push(str);
21+
return;
22+
}
23+
for (const c of map[digits[i]]) {
24+
dfs(i + 1, str + c);
25+
}
26+
};
27+
dfs(0, '');
28+
return res;
29+
}

0 commit comments

Comments
 (0)