242. Valid Anagram | Java最短代码实现

来源:互联网 发布:python os.exit 编辑:程序博客网 时间:2024/06/06 00:37
原题链接:242. Valid Anagram

【思路】

建立一个容量为26大小的int,并将字母转化为int值——将字符串的每个字母char值减去‘a'得到,作为数组索引,字母出现次数作为数组值:

    public boolean isAnagram(String s, String t) {        int[] table = new int[26];        for (int i = 0; i < s.length(); i++)            table[s.charAt(i) - 'a']++;        for (int i = 0; i < t.length(); i++)            table[t.charAt(i) - 'a']--;        for (int temp : table)            if (temp != 0)                return false;        return true;    }
32 / 32 test cases passed. Runtime: 7 ms  Your runtime beats 59.12% of javasubmissions.
欢迎优化!

1 0