mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
cpp: add “242. Valid Anagram”
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
eddb4c1a55
commit
57a9ddef6f
1 changed files with 26 additions and 0 deletions
26
cpp/valid-anagram.cpp
Normal file
26
cpp/valid-anagram.cpp
Normal file
|
@ -0,0 +1,26 @@
|
|||
#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;
|
||||
});
|
||||
}
|
||||
};
|
Loading…
Reference in a new issue