diff --git a/problems/1002.查找常用字符.md b/problems/1002.查找常用字符.md index 78eb1989..fd188660 100644 --- a/problems/1002.查找常用字符.md +++ b/problems/1002.查找常用字符.md @@ -451,6 +451,42 @@ object Solution { } } ``` + +Rust: + +```rust +impl Solution { + pub fn common_chars(words: Vec) -> Vec { + if words.is_empty() { + return vec![]; + } + let mut res = vec![]; + let mut hash = vec![0; 26]; + for i in words[0].bytes() { + hash[(i - b'a') as usize] += 1; + } + for i in words.iter().skip(1) { + let mut other_hash_str = vec![0; 26]; + for j in i.bytes() { + other_hash_str[(j - b'a') as usize] += 1; + } + for k in 0..26 { + hash[k] = hash[k].min(other_hash_str[k]); + } + } + + for (i, v) in hash.iter_mut().enumerate() { + while *v > 0 { + res.push(((i as u8 + b'a') as char).to_string()); + *v -= 1; + } + } + + res + } +} +``` +