🎉 One-stop destination for all your technical interview Preparation 🎉
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
class Solution {
public:
bool isAnagram(string s, string t) {
sort(s.begin(),s.end());
sort(t.begin(),t.end());
return s==t?true:false;
}
};
class Solution {
public:
bool isAnagram(string s, string t) {
vector<int> a(26,0);
if(s.size()!=t.size()) return false;
for(auto &x:s){
a[x-'a']++;
}
for(auto &x:t){
--a[x-'a'];
}
for(auto &x:a){
if(x>=1) return false;
}
return true;
}
};
class Solution {
public:
bool isAnagram(string s, string t) {
vector<int> a(26,0);
if(s.size()!=t.size()) return false;
for(auto &x:s){
a[x-'a']++;
}
for(auto &x:t){
if(--a[x-'a']<0) return false;
}
return true;
}
};