Day 13 — Hashing Techniques

Problem: First Non-Repeating Character in a String

Problem Statement

Given a string s, find and return the first non-repeating character in it. If every character repeats, return None (or -1 in languages that prefer numeric values).

Input / Output Format

Input: A single string s (lowercase and/or uppercase letters; may include digits and symbols).

Output: The first non-repeating character (as a single character) or None / -1 if no such character exists.

Constraints

Examples

Input OutputExplanation
s = "swiss" 'w' — characters: s(3 times), w(1), i(1); first non-repeating scanning left to right → 'w'.
s = "aabbcc" None — every character repeats.
s = "leetcode" 'l' — 'l' occurs once and is the first unique character.
s = "aAb" 'a' — case-sensitive: 'a' and 'A' are different.

Sample Test Cases (copy & run)

# Test case 1
Input: "swiss"
Output: 'w'

# Test case 2
Input: "aabbcc"
Output: None

# Test case 3
Input: "leetcode"
Output: 'l'

# Test case 4
Input: "aAb"
Output: 'a'

Hints (don't peek!)

  1. Use a hash-map / dictionary to count frequencies of characters in one pass.
  2. Scan the string a second time and return the first character with frequency 1.
  3. Alternatively, record the index of first occurrence alongside counts so you can determine the first unique in a single pass over the map.

What to Compare

Include both a brute-force approach (O(n^2)) for clarity and the hashing-based optimized approach (O(n)). Discuss time & space trade-offs.

Follow-ups / Variations (Great for interviews)

Top 10 Common Interview Questions on Hashing

  1. Explain how a hash table works and its average/worst-case complexities.
  2. How do you handle collisions in a hash table? Compare chaining vs. open addressing.
  3. What is a good hash function? What properties should it have?
  4. Difference between Python’s dict and set — how are they implemented internally?
  5. Implement Two Sum problem using hashing. Complexity analysis?
  6. Find the longest substring without repeating characters using hashing.
  7. How to detect if an array contains duplicates using hashing?
  8. Explain load factor in hashing and why resizing is needed.
  9. How does hashing differ from encryption?
  10. Real-world use cases of hashing (caches, compilers, databases, password storage).

Footer

Difficulty: Easy → Medium. Topic: Hashing / Frequency Counting.