710 B
710 B
Notes
DONE 0242. Valid Anagram easy arrays hashing counting
Approach
Frequency counter with fixed-size array (Alpha Frequency Array trick). Early exit on size mismatch. Single pass over both strings.
C++
class Solution {
public:
bool isAnagram(std::string s, std::string t) {
if (s.size() != t.size()) return false;
std::array<int, 26> freq{};
for (int i = 0; i < s.size(); i++) {
freq[s[i] - 'a']++;
freq[t[i] - 'a']--;
}
return std::all_of(freq.begin(), freq.end(), [](int x){ return x == 0; });
}
};