242. Valid Anagram

来源:互联网 发布:js select 赋值选中 编辑:程序博客网 时间:2024/06/03 19:52
// for lowercase alphabets which have 26 letterspublic class Solution {    public boolean isAnagram(String s, String t) {        int[] letters = new int[26];        // set an array for 26 letters        for (int i=0; i<s.length(); i++)    // iterate the first string, count the number for each letter            letters[s.charAt(i) - 'a']++;        for (int j=0; j<t.length(); j++)    // iterate the second string, reduce count by 1 if meets the same letter            letters[t.charAt(j) - 'a']--;        for (int cnt : letters)      // if counts for each letter are zero, then s and t are anagram            if (cnt != 0)                return false;        return true;    }}

0 0