From 90b731607801b9da1b72eab808f3e6baa7f1451e Mon Sep 17 00:00:00 2001 From: Ivan Date: Thu, 8 Feb 2024 17:47:09 +0500 Subject: [PATCH] leetcode: solve problem 17 --- .../Cargo.lock | 7 +++ .../Cargo.toml | 8 +++ .../readme.md | 8 +++ .../src/lib.rs | 60 +++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 leetcode/0017-letter-combinations-of-a-phone-number/Cargo.lock create mode 100644 leetcode/0017-letter-combinations-of-a-phone-number/Cargo.toml create mode 100644 leetcode/0017-letter-combinations-of-a-phone-number/readme.md create mode 100644 leetcode/0017-letter-combinations-of-a-phone-number/src/lib.rs diff --git a/leetcode/0017-letter-combinations-of-a-phone-number/Cargo.lock b/leetcode/0017-letter-combinations-of-a-phone-number/Cargo.lock new file mode 100644 index 0000000..1798698 --- /dev/null +++ b/leetcode/0017-letter-combinations-of-a-phone-number/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "letter-combinations-of-a-phone-number" +version = "0.1.0" diff --git a/leetcode/0017-letter-combinations-of-a-phone-number/Cargo.toml b/leetcode/0017-letter-combinations-of-a-phone-number/Cargo.toml new file mode 100644 index 0000000..b8598e5 --- /dev/null +++ b/leetcode/0017-letter-combinations-of-a-phone-number/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "letter-combinations-of-a-phone-number" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/leetcode/0017-letter-combinations-of-a-phone-number/readme.md b/leetcode/0017-letter-combinations-of-a-phone-number/readme.md new file mode 100644 index 0000000..a6ecfb7 --- /dev/null +++ b/leetcode/0017-letter-combinations-of-a-phone-number/readme.md @@ -0,0 +1,8 @@ +# 17. Letter Combinations of a Phone Number + +[Leetcode](https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/) + +Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. +Return the answer in any order. + +A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. diff --git a/leetcode/0017-letter-combinations-of-a-phone-number/src/lib.rs b/leetcode/0017-letter-combinations-of-a-phone-number/src/lib.rs new file mode 100644 index 0000000..1730c38 --- /dev/null +++ b/leetcode/0017-letter-combinations-of-a-phone-number/src/lib.rs @@ -0,0 +1,60 @@ +pub fn letter_combinations(digits: String) -> Vec { + let mut res: Vec = vec![]; + + for digit in digits.split("") { + if digit.is_empty() { + continue; + } + + let letters = match digit { + "2" => vec!["a", "b", "c"], + "3" => vec!["d", "e", "f"], + "4" => vec!["g", "h", "i"], + "5" => vec!["j", "k", "l"], + "6" => vec!["m", "n", "o"], + "7" => vec!["p", "q", "r", "s"], + "8" => vec!["t", "u", "v"], + "9" => vec!["w", "x", "y", "z"], + &_ => vec![], + }; + + if res.is_empty() { + for letter in letters { + res.push(letter.to_owned()); + } + continue; + } + + let mut mu: Vec = vec![]; + for el in res.iter() { + for letter in &letters { + mu.push(el.to_owned() + letter); + } + } + res = mu; + } + + res +} + +#[cfg(test)] +mod tests { + use super::letter_combinations; + + #[test] + fn simple_test() { + let expected: Vec = vec!["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"] + .iter() + .map(|s| s.to_string()) + .collect(); + let res = letter_combinations("23".to_string()); + assert_eq!(res, expected); + } + + #[test] + fn test_empty_string() { + let expected: Vec = vec![]; + let res = letter_combinations("".to_string()); + assert_eq!(res, expected); + } +}