Skip to content

Commit

Permalink
#19: add solution for exercise #17 in rust
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielvictorcf authored and gosiqueira committed Nov 1, 2022
1 parent 2d2be3f commit 61460be
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions rust/backtracking/0017-letter-combinations-of-a-phone-number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::collections::HashMap;

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

let phone_mapping: HashMap<char, &str> = [
('2', "abc"),
('3', "def"),
('4', "ghi"),
('5', "jkl"),
('6', "mno"),
('7', "pqrs"),
('8', "tuv"),
('9', "wxyz")
].into();

let mut results: Vec<String> = vec![];
for digit in digits.chars() {
let letters = phone_mapping[&digit];
let mut cur_results = vec![];

for letter in letters.chars() {
if results.len() == 0 {
cur_results.push(letter.to_string());
continue;
}

for res in &results {
cur_results.push(res.to_owned() + &letter.to_string());
}
}

results = cur_results;
}

results
}
}

0 comments on commit 61460be

Please sign in to comment.