-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathletter_combinations.rs
98 lines (72 loc) · 2.69 KB
/
letter_combinations.rs
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use std::collections::{HashMap, VecDeque};
pub fn letter_combinations(digits: String) -> Vec<String> {
if digits.is_empty() {
return vec![];
}
// Create keyboard with digits combo
let letters = vec![
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x", "y", "z",
];
let mut dic: HashMap<u32, Vec<String>> = HashMap::new();
let mut total_string = 0;
let mut curr_index = 2;
for alphabet in letters {
match total_string {
0 => {
dic.insert(curr_index, vec![alphabet.to_string()]);
total_string += 1;
}
_ => {
dic.get_mut(&curr_index).unwrap().push(alphabet.to_string());
total_string += 1;
if curr_index == 7 || curr_index == 9 && total_string == 3 {}
let extra_letters = vec![7, 9];
if total_string == 3 && !extra_letters.contains(&curr_index) {
total_string = 0;
curr_index += 1;
}
if extra_letters.contains(&curr_index) && total_string == 4 {
total_string = 0;
curr_index += 1;
}
}
}
}
let mut all_letters = VecDeque::new();
for digit in digits.chars() {
let digit = digit.to_digit(10).unwrap();
if let Some(possible_letters) = dic.get(&digit) {
all_letters.push_back(possible_letters.to_vec());
}
}
while all_letters.len() > 1 {
let first_letters = all_letters.pop_front().unwrap();
let second_letters = all_letters.pop_front().unwrap();
let mut compose = vec![];
for first_string in &first_letters {
for second_string in &second_letters {
let mut the_first = first_string.clone();
the_first.push_str(second_string.as_str());
compose.push(the_first);
}
}
all_letters.push_front(compose);
}
return all_letters[0].to_vec();
}
#[cfg(test)]
mod test_letter_comb_mod {
use crate::recursion::letter_combinations::letter_combinations;
#[test]
fn test_letter_comb() {
let letter_combo = letter_combinations("1234".to_string());
assert_eq!(letter_combo.len(), 27);
let letter_combo = letter_combinations("23".to_string());
assert_eq!(letter_combo.len(), 9);
let letter_combo = letter_combinations("2".to_string());
assert_eq!(letter_combo.len(), 3);
let letter_combo = letter_combinations("".to_string());
assert_eq!(letter_combo.len(), 0);
}
}