upd
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
#+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
|
||||
Reference in New Issue
Block a user