20240508 Finished

This commit is contained in:
jackfiled 2024-05-08 10:01:30 +08:00
parent 00da0dd36f
commit 7f7f30b76f
2 changed files with 51 additions and 1 deletions

View File

@ -124,4 +124,5 @@ mod p73_set_matrix_zeroes;
mod p289_game_of_life; mod p289_game_of_life;
mod p383_ransom_note; mod p383_ransom_note;
mod p290_word_pattern; mod p290_word_pattern;
mod p205_isomorphic_strings; mod p205_isomorphic_strings;
mod p242_valid_anagram;

View File

@ -0,0 +1,49 @@
/**
* [242] Valid Anagram
*/
pub struct Solution {}
// submission codes start here
use std::collections::HashMap;
impl Solution {
pub fn is_anagram(s: String, t: String) -> bool {
let mut map = HashMap::new();
for c in s.chars() {
let entry = map.entry(c).or_insert(0);
*entry += 1;
}
for c in t.chars() {
match map.get_mut(&c) {
Some(value) => {
*value -= 1;
if *value == 0 {
map.remove(&c);
}
},
None => {
return false;
}
}
}
map.len() == 0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_242() {
assert!(Solution::is_anagram("anagram".to_owned(), "nagaram".to_owned()));
}
}