Last active
April 29, 2016 09:56
-
-
Save jacobwalkr/25ef9aff13349aa3c9771b3ce4275dbd to your computer and use it in GitHub Desktop.
Broken solution to https://www.hackerrank.com/challenges/sparse-arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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