• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

Formal Definition

Character hashing use cases are the set of string problems solvable by building a frequency structure (Phase 1) and then applying problem-specific retrieval logic (Phase 2). Common patterns include finding the most frequent character, detecting anagrams, finding the first non-repeating character, checking if a string can be rearranged, and validating character-level constraints.

Explanation

Character hashing is a family of problems, not a single problem. What unifies them is the Phase 1 structure (identical across all problems) and what differentiates them is the Phase 2 retrieval (unique to each problem). Mastering character hashing means recognizing which problems share Phase 1 and knowing how Phase 2 varies.

How It Works

  1. Read the problem: does it involve counting character occurrences?
  2. If yes, Phase 1 is mechanical — build the frequency structure
  3. Determine the Phase 2 pattern:
    • Max/Min: traverse structure, track extreme value
    • Order-sensitive: re-traverse the original string
    • Comparison: build two structures and compare
    • Constraint check: verify counts satisfy a condition (e.g., palindrome rearrangement: at most one odd count)
  4. Apply the pattern to the specific problem

Visual Explanation

use_cases P1 Phase 1: Build Frequency Structure MAX Max: Most Frequent Traverse struct, track max P1->MAX ORDER Order: First Non-Repeat Re-traverse string P1->ORDER COMPARE Compare: Anagram Build two structs P1->COMPARE CHECK Check: Palindrome At most one odd count P1->CHECK

Semantic Network

semantic_use_cases THIS Character Hashing Use Cases PRE1 Two-Phase Hashing THIS--PRE1 built from PRE2 Hashing Retrieval Phase THIS--PRE2 built from OUT1 Interview Decision Framework THIS--OUT1 builds into CON1 Anagram Detection THIS--CON1 contrasts with CON2 Most Frequent Character THIS--CON2 contrasts with CON3 First Non-Repeating Character THIS--CON3 contrasts with REL1 Frequency Array THIS--REL1 related REL2 Unordered Map Frequency THIS--REL2 related

Key Properties

  • All use cases share Phase 1 — the structure-building code is reusable across problems
  • Phase 2 is problem-specific — changing the problem changes only the retrieval logic
  • Problems can be categorized by Phase 2 pattern: max/min, order-sensitive, comparison, constraint
  • The category determines data structure choice (array vs map may differ by use case)
  • Recognition of the pattern is the skill — once Phase 2 is categorized, coding is mechanical

Connections

Edge Cases & Gotchas

  • Not all string problems are hashing problems — substring search, pattern matching, and edit distance use different techniques
  • Some problems can be solved in one pass (tracking max while building frequencies) — the two-phase model is conceptual, not always sequential
  • For palindrome rearrangement: the constraint is “at most one character has odd count” — this Phase 2 check is a simple filter
  • For “character that appears more than n/2 times” (majority element): Boyer-Moore voting is more efficient than hashing — recognizing when NOT to hash is also important
  • For problems with very large alphabets (Unicode), hashing is necessary but may benefit from specialized data structures (e.g., trie for prefix frequencies)