Letter Combinations of a Phone Number LeetCode solution in Rust

1
impl Solution {
2
pub fn letter_combinations(digits: String) -> Vec<String> {
3
if digits.is_empty() {
4
return vec![];
5
}
6

7
let phone_map = vec!["abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"];
8
let mut output = Vec::new();
9

10
fn backtrack(
11
combination: String,
12
next_digits: &str,
13
phone_map: &Vec<&str>,
14
output: &mut Vec<String>,
15
) {
16
if next_digits.is_empty() {
17
output.push(combination);
18
} else {
19
let letters = phone_map[next_digits.chars().nth(0).unwrap() as usize - '2' as usize];
20
for letter in letters.chars() {
21
let new_combination = combination.clone() + &letter.to_string();
22
backtrack(new_combination, &next_digits[1..], phone_map, output);
23
}
24
}
25
}
26

27
backtrack(String::new(), &digits, &phone_map, &mut output);
28
output
29
}
30
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0