ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
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 class Solution { public boolean isAnagram(String s, String t) { int[] map = new int[26]; char[] cs = s.toCharArray(); for(char c : cs) { int x = c - 'a'; map[x] += 1; } cs = t.toCharArray(); for(char c : cs) { int x = c - 'a'; if(map[x] == 0) { return false; } map[x] -= 1; } for(int x : map) { if(x != 0) { return false; } } return true; } } //是否字母顺序不一样,用一个哈希表记录其中一个字符串的每一个字符及 //出现的次数。用另一个字符串的字符当作key获取value判断是否存在。 ```