| 1 | pub fn subdomain_visits(cpdomains: Vec<String>) -> Vec<String> { |
| 2 | use std::collections::HashMap; |
| 3 | let mut mmp: HashMap<String, usize> = HashMap::new(); |
| 4 | |
| 5 | for c in cpdomains { |
| 6 | let arr: Vec<String> = c.split(" ").map(|e|e.to_string()).collect(); |
| 7 | let num: usize = arr[0].parse().unwrap(); |
| 8 | let url_arr: Vec<String> = arr[1].split(".").map(|e|e.to_string()).collect(); |
| 9 | if url_arr.iter().count() == 3 { |
| 10 | let three = url_arr.join("."); |
| 11 | let count = mmp.entry(three.to_string()).or_insert(0); |
| 12 | *count += num; |
| 13 | let two = url_arr[1..].join("."); |
| 14 | let count = mmp.entry(two.to_string()).or_insert(0); |
| 15 | *count += num; |
| 16 | let one = &url_arr[2]; |
| 17 | let count = mmp.entry(one.to_string()).or_insert(0); |
| 18 | *count += num; |
| 19 | } |
| 20 | if url_arr.iter().count() == 2 { |
| 21 | let two = url_arr.join("."); |
| 22 | let count = mmp.entry(two.to_string()).or_insert(0); |
| 23 | *count += num; |
| 24 | let one = &url_arr[1]; |
| 25 | let count = mmp.entry(one.to_string()).or_insert(0); |
| 26 | *count += num; |
| 27 | } |
| 28 | if url_arr.iter().count() == 1 { |
| 29 | let one = &url_arr[0]; |
| 30 | let count = mmp.entry(one.to_string()).or_insert(0); |
| 31 | *count += num; |
| 32 | } |
| 33 | } |
| 34 | let mut ret: Vec<String> = vec!(); |
| 35 | for (k, v) in mmp { |
| 36 | let mut tmp = String::new(); |
| 37 | tmp.push_str(&v.to_string()); |
| 38 | tmp.push(' '); |
| 39 | tmp.push_str(&k); |
| 40 | ret.push(tmp); |
| 41 | } |
| 42 | ret |
| 43 | } |
| 44 | |
| 45 | fn main() { |
| 46 | let cpdomains = vec!["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"] |