Skip to content

Commit 4529840

Browse files
committed
8-5-2020
1 parent 7ef0f50 commit 4529840

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
struct Solution;
2+
3+
use std::collections::HashMap;
4+
5+
impl Solution {
6+
pub fn letter_combinations(digits: String) -> Vec<String> {
7+
if digits.is_empty() {
8+
return vec![];
9+
}
10+
11+
let h_map: HashMap<char, Vec<char>> = [
12+
('2', "abc"),
13+
('3', "def"),
14+
('4', "ghi"),
15+
('5', "jkl"),
16+
('6', "mno"),
17+
('7', "pqrs"),
18+
('8', "tuv"),
19+
('9', "wxyz"),
20+
('0', " "),
21+
]
22+
.iter()
23+
.map(|(number, letters)| (*number, letters.chars().collect()))
24+
.collect();
25+
26+
let mut current_combination: Vec<char> = vec![];
27+
let current_index: usize = 0;
28+
let mut result: Vec<String> = vec![];
29+
let digits: Vec<char> = digits.chars().collect();
30+
Self::dfs(
31+
&h_map,
32+
&digits,
33+
&mut current_combination,
34+
current_index,
35+
&mut result,
36+
);
37+
result
38+
}
39+
40+
fn dfs(
41+
h_map: &HashMap<char, Vec<char>>,
42+
digits: &Vec<char>,
43+
current_combination: &mut Vec<char>,
44+
current_index: usize,
45+
result: &mut Vec<String>,
46+
) {
47+
if current_index == digits.len() {
48+
result.push(current_combination.iter().collect::<String>());
49+
} else {
50+
let current_digit: char = digits[current_index];
51+
for &c in h_map[&current_digit].iter() {
52+
current_combination.push(c);
53+
Self::dfs(
54+
h_map,
55+
digits,
56+
current_combination,
57+
current_index + 1,
58+
result,
59+
);
60+
current_combination.pop();
61+
}
62+
}
63+
}
64+
}
65+
66+
#[test]
67+
fn test() {
68+
assert_eq!(
69+
Solution::letter_combinations("23".to_string()),
70+
vec_string!["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
71+
)
72+
}

src/_0038_count_and_say.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn count_and_say(n: i32) -> String {
5+
if n == 1 {
6+
return "1".to_string();
7+
}
8+
9+
let mut previous: Vec<char> = Self::count_and_say(n - 1).chars().collect();
10+
previous.push(' ');
11+
12+
let mut result: String = "".to_string();
13+
let mut digit_read: char = previous[0];
14+
let mut digit_read_count: u32 = 1;
15+
16+
for index in 1..previous.len() {
17+
if previous[index] == digit_read {
18+
digit_read_count += 1;
19+
} else {
20+
result.push(std::char::from_digit(digit_read_count, 10).unwrap());
21+
result.push(digit_read);
22+
digit_read = previous[index].clone();
23+
digit_read_count = 1;
24+
}
25+
}
26+
result
27+
}
28+
}
29+
30+
#[test]
31+
fn test() {
32+
assert_eq!(Solution::count_and_say(1), "1".to_string());
33+
assert_eq!(
34+
Solution::count_and_say(30),
35+
"3113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112212211131221121321131211132221123113112221131112311332211211133112111311222112111312211311123113322112111312211312111322212321121113121112133221121321132132211331121321132213211231132132211211131221232112111312212221121123222112311311222113111231133211121321321122111312211312111322211213211321322123211211131211121332211231131122211311123113321112131221123113111231121123222112111331121113112221121113122113111231133221121113122113121113221112131221123113111231121123222112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321132132211322132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211231131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122211211133112111311222112111312211312111322211213211321223112111311222112132113213221133122211311221122111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321132132211322132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331222113321112131122211332113221122112133221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213122112311311123112112322211322311311222113111231133211121312211231131112311211232221121113122113121113222123211211131221132211131221121321131211132221123113112211121312211231131122113221122112133221121321132132211331121321231231121113121113122122311311222113111231133221121113122113121113221112131221123113111231121123222112132113213221133112132123123112111312211322311211133112111312211213211311123113223112111321322123122113222122211211232221121113122113121113222123211211131211121311121321123113213221121113122123211211131221121311121312211213211321322112311311222113311213212322211211131221131211221321123113213221121113122113121113222112131112131221121321131211132221121321132132211331121321232221123113112221131112311322311211131122211213211331121321122112133221121113122113121113222123112221221321132132211231131122211331121321232221121113122113121113222123211211131211121332211213111213122112132113121113222112132113213221232112111312111213322112132113213221133112132123123112111311222112132113311213211221121332211231131122211311123113321112131221123113112221132231131122211211131221131112311332211213211321223112111311222112132113212221132221222112112322211211131221131211132221232112111312111213111213211231131112311311221122132113213221133112132123222112311311222113111231132231121113112221121321133112132112211213322112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121311121312211213211312111322211213211321322123211211131211121332211213211321322113311213211322132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113111231133221121321132122311211131122211213211321222113222122211211232221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213213211221113122113121113222112132113213221232112111312111213322112132113213221133112132123123112111312211322311211133112111312212221121123222112132113213221133112132123222113223113112221131112311332111213122112311311123112112322211211131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112211322212322211231131122211322111312211312111322211213211321322113311213211331121113122122211211132213211231131122212322211331222113112211".to_string()
36+
);
37+
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ mod _0014_longest_common_prefix;
2828
//
2929
mod _0015_3sum;
3030
//
31+
mod _0017_letter_combinations_of_a_phone_number;
32+
//
3133
mod _0020_valid_parentheses;
3234
//
3335
mod _0021_merge_two_sorted_lists;
@@ -38,6 +40,8 @@ mod _0034_find_first_and_last_position_of_element_in_sorted_array;
3840
//
3941
mod _0036_valid_sudoku;
4042
//
43+
mod _0038_count_and_say;
44+
//
4145
mod _0046_permutations;
4246
//
4347
mod _0053_maximum_subarray;

0 commit comments

Comments
 (0)