Beyond Big O

First unpaired event

First unpaired event

Easy

Solution guide

Reveal solution guide — spoilers

Approach

  1. Normalize ASCII A–Z to a–z and count each normalized key in a frequency map. Preserve all other allowed characters; do not trim or parse identifiers.
  2. Scan the original sequence a second time, normalizing each key again. Return the first normalized key whose final count is exactly one.
  3. If the second scan finds no such key, return the empty string. Keep the map local to the call and leave the input unchanged.

Why it works

After counting, the map contains every occurrence, including repeats far apart. The second scan considers keys in encounter order and tests their complete frequency, so its first qualifying position is exactly the required first unique key. Map iteration order is irrelevant.

Complexity

Expected O(C) time for C total input characters, including both normalization passes and hashing. Retained normalized keys and counts use O(D + K) auxiliary space for D distinct normalized keys containing K characters in total.

Edge cases to check

  • Empty input and inputs where every normalized key repeats return the empty string.
  • The keys "API" and "api" share one count; underscores, hyphens and digits remain unchanged.
  • A later duplicate disqualifies an earlier candidate, so returning during the counting pass is unsafe.
Editor options
Typing preferences

Saved in this browser.

Restoring local drafts…