Given two strings s and t , write a function to determine if t is an anagram of s.
Input: s = "anagram", t = "nagaram" Output: true
Input: s = "rat", t = "car" Output: false
You may assume the string contains only lowercase alphabets.
What if the inputs contain unicode characters? How would you adapt your solution to such case?
impl Solution {
pub fn is_anagram(s: String, t: String) -> bool {
if s.len() != t.len() {
return false;
}
let mut s_vec: Vec<char> = s.chars().collect();
let mut t_vec: Vec<char> = t.chars().collect();
s_vec.sort_unstable();
t_vec.sort_unstable();
s_vec == t_vec
}
}
use std::collections::HashMap;
impl Solution {
pub fn is_anagram(s: String, t: String) -> bool {
if s.len() != t.len() {
return false;
}
let s_vec: Vec<char> = s.chars().collect();
let t_vec: Vec<char> = t.chars().collect();
let mut map = HashMap::new();
for i in 0..s_vec.len() {
*map.entry(s_vec[i]).or_insert(0) += 1;
*map.entry(t_vec[i]).or_insert(0) -= 1;
}
map.values().filter(|x| **x != 0).count() == 0
}
}