22 lines
608 B
Org Mode
22 lines
608 B
Org Mode
|
|
#+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.
|