• + 0 comments

    rust answer

    fn anagram(s: &str) -> i32 {
        let mut counter = vec![0;26];
        let chs: Vec<char> = s.chars().collect();
        let mut res = 0;
        let mut idx:usize = 0;
        
        if chs.len() % 2 == 1 {
            return -1;
        }
        
        for i in 0..chs.len()/2 {
            idx = (chs[i] as u32 - 'a' as u32) as usize;
            counter[idx] = counter[idx] + 1;
        }
        for i in chs.len()/2..chs.len() {
            idx = (chs[i] as u32 - 'a' as u32) as usize;
            if counter[idx] == 0 {
                res += 1;
            } else {
                counter[idx] = counter[idx] - 1;
            }
        }
        
        res
    
    }