This commit is contained in:
2026-06-01 18:12:40 +08:00
parent c9fe2fab76
commit 8c9f58fdfb
5 changed files with 168 additions and 10 deletions
+21
View File
@@ -0,0 +1,21 @@
#+ANKI_DECK: study_deck_02
#+TITLE: DSA Tricks & Patterns
* Task: Count character frequencies faster than map :dsa:counting:array:string:retrieval::production:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ROADMAP: [[file:../roadmap.org::*0242. Valid Anagram][0242. Valid Anagram]]
:END:
** Front
Write C++ to count character frequencies in a string using a
fixed-size array instead of ~std::map~ (assume lowercase a-z only).
** Back
#+begin_src cpp
std::array<int, 26> freq{};
for (char c : s) freq[c - 'a']++;
#+end_src
*Why:* O(1) per access vs O(log n) for ~std::map~. Use when alphabet
is bounded and small.