码迷,mamicode.com
首页 > 其他好文 > 详细

isAnagram

时间:2015-09-17 23:18:33      阅读:226      评论:0      收藏: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;
    }
}

 

isAnagram

标签:

原文地址:http://www.cnblogs.com/kydnn/p/4817710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!