242. 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.

Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?

//通过建立一个类似c中的字符型数组,s中出现一个加1,t中出现一次减一,最后检查是否数组中所有的元素都是0即可

public class Solution {
    public boolean isAnagram(String s, String t) {
        if(s.length()!=t.length())
            return false;
        
        int []result = new int [256];
        for(int i=0; i < s.length(); i++){
            result[s.charAt(i)-'a']++;
            result[t.charAt(i)-'a']--;
        }
        
        
        
        for(int i=0; i< 256;i++){
            if(result[i]!= 0){
                return false;
            }
        }
        return true;
    }
}