Es 01 Anagram

This commit is contained in:
2024-04-30 19:07:25 +02:00
parent 8fe7754375
commit 20978d583c
4 changed files with 55 additions and 0 deletions

19
src/es01_anagram.rs Normal file
View File

@@ -0,0 +1,19 @@
#![allow(dead_code)]
use std::collections::HashMap;
/** Es.1
* Scrivere una funzione che prende in input due riferimenti a stringhe e ritorna
* true se le stringhe con anagramma una dellaltra e false altrimenti.
*/
pub fn anagrammi(str1: &str, str2: &str) -> bool {
if str1.len() != str2.len() {
false
} else {
let mut map = HashMap::new();
str1.chars().for_each(|x| *map.entry(x).or_insert(0) += 1);
str2.chars().for_each(|x| *map.entry(x).or_insert(0) -= 1);
map.iter().all(|(_, x)| *x == 0)
}
}

1
src/lib.rs Normal file
View File

@@ -0,0 +1 @@
pub mod es01_anagram;