From 6a5f3eccaf66944f556b4fe78c8284ff75b21c99 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Thu, 2 Jun 2022 23:45:04 +0100 Subject: [PATCH] valid-anagram --- valid-anagram/sol.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 valid-anagram/sol.rs diff --git a/valid-anagram/sol.rs b/valid-anagram/sol.rs new file mode 100644 index 0000000..01fe551 --- /dev/null +++ b/valid-anagram/sol.rs @@ -0,0 +1,17 @@ +// For unicode hashmaps may be used +impl Solution { + pub fn is_anagram(s: String, t: String) -> bool { + let mut s_map = s.bytes() + .fold([0; 26], |mut map, chr| { + map[chr as usize - 97] += 1; + map + }); + t.bytes() + .fold(s_map, |mut map, chr| { + map[chr as usize - 97] -= 1; + map + }) + .iter() + .all(|&count| count == 0) + } +} \ No newline at end of file