Valid Anagram

Given two strings s and t, write a function to determine if t is an anagram of s.

For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.

Note:
You may assume the string contains only lowercase alphabets.

class Solution {
public:
    bool isAnagram(string s, string t) {
       if (s.length() != t.length()) return false;
          int n = s.length();
          int counts[26] = {0};
          for (int i = 0; i < n; i++) { 
              counts[s[i] - 'a']++;
              counts[t[i] - 'a']--;
         }
         for (int i = 0; i < 26; i++)
            if (counts[i]) return false;
         return true;
    }
};



    public boolean isAnagram(String s, String t) {
        if(s==null||t==null||s.length()!=t.length()){
            return false;
        }
        char[] array1 = s.toCharArray();
        char[] array2 = t.toCharArray();
        Arrays.sort(array1);
        Arrays.sort(array2);
        return Arrays.equals(array1, array2);
    }


你可能感兴趣的:(Valid Anagram)