-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.rs
38 lines (32 loc) · 965 Bytes
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use std::char::ParseCharError;
fn main() {
println!("Hello, world!");
}
struct Solution {}
impl Solution {
pub fn most_common_word(paragraph: String, banned: Vec<String>) -> String {
let mut words = std::collections::HashMap::new();
for word in paragraph.split(['!', '?', ' ', '\'', ',', '.', ';']) {
if word.len() == 0 { continue }
let word = word.to_lowercase();
if banned.contains(&word) { continue }
let x = words.entry(word).or_insert(0);
*x += 1;
}
if let Some((a,_)) = words.iter().max_by_key(|x| x.1) {
a.to_owned()
} else { unimplemented!() }
}
}
#[cfg(test)]
mod test {
use std::vec;
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::most_common_word(
"Bob hit a ball, the hit BALL flew far after it was hit.".to_owned(),
vec!["hit".to_owned()]
),"ball");
}
}