Given two strings s and t , write a function to determine if t is an anagram of s.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
最后不是判断数组size == 0,因为int[]的size固定是26。
应该判断每个元素的值是否=0
class Solution { public boolean isAnagram(String s, String t) { //cc,太麻烦了先不写了吧 //新建数组 int[] chars = new int[26]; //先存s for (int i = 0; i < t.length(); i++) { chars[t.charAt(i) - 'a']++; } //再减t for (int j = 0; j < s.length(); j++) { chars[s.charAt(j) - 'a']--; } //判断 System.out.println("chars.length = " + chars.length); for (int k = 0; k < 26; k++) { if (chars[k] != 0) return false; } return true; } }