LeetCode/cpp/valid-anagram.cpp
Matej Focko 57a9ddef6f
cpp: add “242. Valid Anagram”
Signed-off-by: Matej Focko <me@mfocko.xyz>
2023-12-17 18:36:53 +01:00

26 lines
532 B
C++

#include <algorithm>
#include <array>
#include <string>
class Solution {
public:
bool isAnagram(const std::string& s, const std::string& t) {
std::array<int, 26> counter{};
for (char c : s) {
counter[c - 'a']++;
}
for (char c : t) {
counter[c - 'a']--;
if (counter[c - 'a'] < 0) {
return false;
}
}
return std::all_of(counter.begin(), counter.end(), [](auto c) {
return c == 0;
});
}
};