upd
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
#+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]]
|
||||
:ANKI_NOTE_ID: 1780308820578
|
||||
:END:
|
||||
|
||||
** Front
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_ID: 1780308820574
|
||||
:END:
|
||||
Write C++ to count character frequencies in a string using a
|
||||
fixed-size array instead of ~std::map~ (assume lowercase a-z only).
|
||||
|
||||
** Back
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_ID: 1780308820567
|
||||
:END:
|
||||
#+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.
|
||||
Reference in New Issue
Block a user