Files
cpp-flashcards/org/study_deck_02/toolkit/notes.org
T

28 lines
710 B
Org Mode
Raw Normal View History

2026-06-01 18:12:40 +08:00
#+ANKI_DECK: study_deck_02
#+title: Notes
* DONE 0242. Valid Anagram :easy:arrays:hashing:counting:
:PROPERTIES:
:ROADMAP: [[file:../roadmap.org::*0242. Valid Anagram][0242. Valid Anagram]]
:END:
** Approach
Frequency counter with fixed-size array (Alpha Frequency Array trick).
Early exit on size mismatch. Single pass over both strings.
** C++
#+begin_src cpp
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; });
}
};
#+end_src