cKK

............当你觉得自己很辛苦,说明你正在走上坡路.............坚持做自己懒得做但是正确的事情,你就能得到别人想得到却得不到的东西............

导航

isAnagram

Posted on 2015-09-17 21:26  cKK  阅读(441)  评论(0编辑  收藏  举报
/*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.*/
public static boolean isAnagram(String s, String t) {
        int[] ch1 = new int[26];
        char[] cha1 = s.toCharArray();
        char[] cha2 = t.toCharArray();
        if (cha1.length != cha2.length)
            return false;
        for (int i = 0; i < cha1.length; i++) {
            int temp = cha1[i] - 97;
            ch1[temp]++;
        }
        for (int i = 0; i < cha2.length; i++) {
            int temp = cha2[i] - 97;
            ch1[temp]--;
        }
        for (int i : ch1) {
            if (i != 0)
                return false;
        }
        return true;
    }
}