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.

Node
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?


给定字符串st所有的字符都是小写,判断字符串是否为相同字母异序词

public boolean isAnagram(String s, String t) {
        int M = s.length();
        int N = t.length();
        if(M != N) 
            return false;
        int sArray[] = new int[128];
        int tArray[] = new int[128];
        for(int i = 0; i < M; i++)
        {
            sArray[s.charAt(i) - 'a']++;
            tArray[t.charAt(i) - 'a']++;
        }
        for(int i = 0; i < sArray.length; i++)
        {
            if(sArray[i] != tArray[i])
                return false;
        }
        return true;
        
    }

posted on 2017-10-19 19:49  Wanna_Go  阅读(98)  评论(0编辑  收藏  举报

导航