242_ValidAnagram - a920604a/leetcode GitHub Wiki
title: 242. Valid Anagram
class Solution {
public:
bool isAnagram(string s, string t) {
sort(s.begin(), s.end());
sort(t.begin(), t.end());
return s==t;
}
};
class Solution {
public:
bool isAnagram(string s, string t) {
vector<int> vec(26,0);
for(char c:s) vec[c-'a']++;
for(char c:t) vec[c-'a']--;
return vec==vector<int>(26,0);
}
};
- option 1
- time complexity
O(n)
- space complexity
O(1)
- time complexity
- option 2
- time complexity
O(n)
- space complexity
O(1)
- time complexity