-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathphone_keypad_combinations.rs
More file actions
56 lines (52 loc) · 1.57 KB
/
phone_keypad_combinations.rs
File metadata and controls
56 lines (52 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use std::collections::HashMap;
fn backtrack(
i: usize,
curr_combination: &mut Vec<char>,
digits: &[char],
keypad_map: &HashMap<char, &str>,
res: &mut Vec<String>,
) {
// Termination condition: if all digits have been considered
// Add the current combination to the output list
if curr_combination.len() == digits.len() {
res.push(curr_combination.iter().collect());
return;
}
// PYTHON = for letter in keypad_map[digits[i]]...
if let Some(&digit) = digits.get(i) {
if let Some(letters) = keypad_map.get(&digit) {
for letter in letters.chars() {
// Add current letter
curr_combination.push(letter);
// Recursively explore all paths that branch from this combination.
backtrack(i + 1, curr_combination, digits, keypad_map, res);
// Backtrack by removing the letter we just added.
curr_combination.pop();
}
}
}
}
fn phone_keypad_combinations(digits: &str) -> Vec<String> {
let index = 0;
let mut curr_combination = Vec::new();
let digits_chars: Vec<char> = digits.chars().collect(); // collect chars once
let keypad_map = HashMap::from([
('2', "abc"),
('3', "def"),
('4', "ghi"),
('5', "jkl"),
('6', "mno"),
('7', "pqrs"),
('8', "tuv"),
('9', "wxyz"),
]);
let mut res = Vec::new();
backtrack(
index,
&mut curr_combination,
&digits_chars,
&keypad_map,
&mut res,
);
res
}