Given two strings s _and _t , write a function to determine if t _is an anagram of _s.

class Solution {
    public boolean isAnagram(String s, String t) {
        if(s == null && t == null)
            return true;
        if(s == null || t == null)
            return false;

        if(s.length() != t.length())
            return false;

        int[] count = new int[26];
        for(int i = 0; i < s.length(); i++)
            count[s.charAt(i) - 'a']++;

        for(int i = 0; i < t.length(); i++)
            count[t.charAt(i) - 'a']--;

        for(int i = 0; i < 26; i++) 
            if(count[i] != 0)
                return false;

        return true;
    }
}

results matching ""

    No results matching ""