-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0819_most_common_word.rs
More file actions
37 lines (34 loc) · 992 Bytes
/
s0819_most_common_word.rs
File metadata and controls
37 lines (34 loc) · 992 Bytes
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
#![allow(unused)]
pub struct Solution {}
// amazon interview
use std::collections::{HashSet, HashMap};
use std::iter::FromIterator;
impl Solution {
pub fn most_common_word(paragraph: String, banned: Vec<String>) -> String {
let mut banset: HashSet<String> = HashSet::from_iter(banned);
let mut freq = HashMap::new();
let words = paragraph.split(|c: char| c.is_ascii_punctuation() || c.is_ascii_whitespace());
for word in words {
let word = word.to_ascii_lowercase();
if word == "" || banset.contains(&word) {
continue;
}
*freq.entry(word).or_insert(0) += 1;
}
let mut most = "";
let mut most_count = 0;
for (k, v) in freq.iter() {
if v > &most_count {
most_count = *v;
most = k
}
}
most.to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_819() {}
}