Skip to content

Commit 84d57bd

Browse files
committed
7-22-2020
1 parent 3915a11 commit 84d57bd

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/_0819_most_common_word.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use std::collections::HashMap;
2+
use std::collections::HashSet;
3+
4+
struct Solution;
5+
6+
impl Solution {
7+
pub fn most_common_word(paragraph: String, banned: Vec<String>) -> String {
8+
let paragraph = paragraph
9+
.replace(",", " ")
10+
.replace(".", " ")
11+
.replace("!", " ")
12+
.replace("?", " ")
13+
.replace("'", " ")
14+
.replace(";", " ");
15+
let words: Vec<&str> = paragraph.split_ascii_whitespace().collect();
16+
let mut h_map_words: HashMap<String, u32> = HashMap::new();
17+
let h_map_banned: HashSet<String> = banned.into_iter().collect();
18+
19+
for word in words {
20+
let w = word.to_string().to_lowercase();
21+
if !h_map_banned.contains(&w) {
22+
let count = h_map_words.entry(w).or_insert(0);
23+
*count += 1;
24+
}
25+
}
26+
27+
let mut max_word: String = "".to_string();
28+
let mut max_count: u32 = 0;
29+
for (v, i) in h_map_words {
30+
if max_count < i {
31+
max_count = i;
32+
max_word = v.to_string();
33+
}
34+
}
35+
max_word
36+
}
37+
}
38+
39+
#[test]
40+
fn test() {
41+
assert_eq!(
42+
Solution::most_common_word(
43+
"Bob hit a ball, the hit BALL flew far after it was hit.".to_string(),
44+
vec_string!["hit"]
45+
),
46+
"ball".to_string()
47+
);
48+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ mod _0771_jewels_and_stones;
126126
//
127127
mod _0796_rotate_string;
128128
//
129+
mod _0819_most_common_word;
130+
//
129131
mod _0844_backspace_string_compare;
130132
//
131133
mod _0905_sort_array_by_parity;

0 commit comments

Comments
 (0)