-
BingoPython解法一:
class Solution: # @param {string} s # @param {string} t # @return {boolean} def isAnagram(self, s, t): return sorted(s) == sorted(t)
-
BingoPython解法二:
class Solution: # @param {string} s # @param {string} t # @return {boolean} def isAnagram(self, s, t): from collections import Counter return Counter(s).items() == Counter(t).items()
-
BingoC++解法一:
class Solution { public: bool isAnagram(string s, string t) { sort(s.begin(), s.end()); sort(t.begin(), t.end()); return s == t; } };
-
BingoC++解法二:
class Solution { public: bool isAnagram(string s, string t) { vector<int> count(26, 0); for(int i = 0; i < s.size(); i ++) count[s[i]-'a'] ++; for(int i = 0; i < t.size(); i ++) count[t[i]-'a'] --; for(int i = 0; i < 26; i ++) if(count[i] != 0) return false; return true; } };
-
BingoJava解法一:
public boolean isAnagram(String s, String t) { if (s.length() != t.length()) { return false; } char[] str1 = s.toCharArray(); char[] str2 = t.toCharArray(); Arrays.sort(str1); Arrays.sort(str2); return Arrays.equals(str1, str2); }
-
BingoJava解法二:
public boolean isAnagram(String s, String t) { if (s.length() != t.length()) { return false; } int[] counter = new int[26]; for (int i = 0; i < s.length(); i++) { counter[s.charAt(i) - 'a']++; counter[t.charAt(i) - 'a']--; } for (int count : counter) { if (count != 0) { return false; } } return true; }
-