#include <string>
#include <vector>
using namespace std;
class Solution {
public:
bool isAnagram(string s, string t) {
vector<int> freq(26, 0);
for (char ch : s) {
freq[ch - 'a']++;
}
for (char ch : t) {
freq[ch - 'a']--;
}
for (int count : freq) {
if (count != 0)
return false;
}
return true;
}
};
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
return(''.join(sorted(s))=="".join(sorted(t)))
This is far better:
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
list_count = [0]*26
for ch in s:
list_count[ord(ch)-ord("a")]+=1
for ch in t:
list_count[ord(ch)-ord("a")]-=1
return all(val==0 for val in list_count)
-> Easy solution but not efficient at all.