Skip to content

Instantly share code, notes, and snippets.

@jacobwalkr
Last active April 29, 2016 09:56
Show Gist options
  • Save jacobwalkr/25ef9aff13349aa3c9771b3ce4275dbd to your computer and use it in GitHub Desktop.
Save jacobwalkr/25ef9aff13349aa3c9771b3ce4275dbd to your computer and use it in GitHub Desktop.
use std::io;
use std::collections::HashMap;
use std::borrow::ToOwned;
fn get_number() -> u32 {
let mut number = String::new();
io::stdin().read_line(&mut number).expect("expected number");
return number.trim().parse::<u32>().ok().expect("expected number");
}
fn get_string() -> String {
let mut input = String::new();
io::stdin().read_line(&mut input).expect("expected string input");
return input.trim().to_owned();
}
fn main() {
let input_count = get_number();
let mut strings: HashMap<String, u32> = HashMap::new();
// Enter the strings into the map, updating if they already exist
for _ in 0..input_count {
let input_string = get_string();
let string_entry = strings.entry(input_string).or_insert(0);
*string_entry += 1;
}
// Return the number of occurrences for each test string
for _ in 0..get_number() {
let test_string = get_string();
match strings.get(&test_string) {
Some(count) => println!("{}", count),
None => println!("0")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment