/** * [17] Letter Combinations of a Phone Number */ pub struct Solution {} // submission codes start here impl Solution { pub fn letter_combinations(digits: String) -> Vec { let mut result = vec![]; let digits: Vec = digits.chars() .map(|c| (c.to_digit(10).unwrap() - 2) as usize) .collect(); let mut str = Vec::with_capacity(digits.len()); Self::search(&digits, &mut str, &mut result, 0); result } fn search(digits: &Vec, str: &mut Vec, result: &mut Vec, pos: usize) { let map = vec![ vec!['a', 'b', 'c'], vec!['d', 'e', 'f'], vec!['g', 'h', 'i'], vec!['j', 'k', 'l'], vec!['m', 'n', 'o'], vec!['p', 'q', 'r', 's'], vec!['t', 'u', 'v'], vec!['w', 'x', 'y', 'z'], ]; if pos >= digits.len() { if !str.is_empty() { result.push(str.iter().collect()); } return; } for &c in &map[digits[pos]] { str.push(c); Self::search(digits, str, result, pos + 1); str.pop(); } } } // submission codes end #[cfg(test)] mod tests { use super::*; #[test] fn test_17() {} }