MCPcopy Create free account
hub / github.com/StudyRust/leetcode_rust / shortest_completing_word

Function shortest_completing_word

748-shortest-completing-word.rs:1–35  ·  view source on GitHub ↗
(license_plate: String, words: Vec<String>)

Source from the content-addressed store, hash-verified

1pub fn shortest_completing_word(license_plate: String, words: Vec<String>) -> String {
2 use std::collections::HashMap;
3 let mut counter_map = HashMap::new();
4 let mut str_map = HashMap::new();
5
6 for c in license_plate.chars() {
7 if c.is_alphabetic() {
8 let coun = str_map.entry(c.to_lowercase().to_string()).or_insert(0);
9 *coun += 1;
10 }
11 }
12
13 'outer: for word in words {
14 let mut word_map = HashMap::new();
15 for c in word.chars() {
16 let coun = word_map.entry(c.to_string()).or_insert(0);
17 *coun += 1;
18 }
19 for (k, v) in &str_map {
20 if word_map.get(k) == None || word_map[k] != *v {
21 continue 'outer;
22 }
23 }
24 counter_map.entry(word.len()).or_insert(Vec::new()).push(word);
25 }
26
27 let mut counter_vec: Vec<_> = counter_map.iter().collect();
28 counter_vec.sort_by(|a, b|a.0.cmp(b.0));
29 println!("{:?}", counter_map);
30 if counter_vec.iter().count() == 0 {
31 String::new()
32 } else {
33 counter_vec[0].1[0].to_string()
34 }
35}
36
37fn main() {
38 let license_plate = "GrC8950".to_string();

Callers

nothing calls this directly

Calls 2

getMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected