Tally Unique Words Using Your Own Open-Addressing Table
Data Structure DesignAuto-graded · 23 test casesLast updated
Count unique strings in a list by building a custom hash table with probing, without using built-in sets, dictionaries, or similar lookup containers.
You are given a list of strings. Return how many different strings it contains.
Two strings are the same only if they match exactly, character for character. Case matters, and no normalization is applied. The empty string "" is an ordinary value and counts as one distinct string if it appears.
The twist
You must not use built-in hashing containers such as set, dict, frozenset, collections.Counter, or similar ready-made lookup structures. Build the lookup structure yourself. You may use plain lists, strings, and integers.
Your table should:
- compute a hash value for each string using a function you write,
- resolve collisions in a way you choose (for example linear probing or chaining),
- compare the full strings only when needed, such as when two hash values are equal,
- be sized from the input length so that it stays reasonably sparse.
Task
Implement the function so that it returns the number of distinct strings in the input list.
Constraints
0 <= len(strings) <= 10^6- Each string may be empty or of any length; the total number of characters is of a size that fits in memory.
- Comparison is exact and case-sensitive.
- An empty list returns
0. - If every string is identical, the answer is
1. - Built-in sets, dicts, and similar containers must not be used for the lookup.
Examples
Example 1
Input: count_unique_strings(['apple', 'banana', 'apple', 'cherry', 'banana'])
Output: 3
Example 2
Input: count_unique_strings([''])
Output: 1
Example 3
Input: count_unique_strings(['a', 'b', 'c', 'a', 'b'])
Output: 3
Hints
Hint 1
Pick a table capacity that is a power of two and comfortably larger than the number of strings, so that you can map a hash to a slot with a bit mask.
Hint 2
Use open addressing: start at the slot given by the hash, and step forward (wrapping around) until you find either an empty slot or a slot holding the same string.
Hint 3
Store each string's full hash next to it, and compare the hashes first. Only when they match do you compare the actual strings. Increase the counter only when you place a string into an empty slot.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Build an open-addressing hash table with linear probing.
- If the list is empty, return
0. - Choose a capacity that is a power of two and large enough that the load factor stays at or below about 0.7. Because the number of strings is known up front, the table never needs to grow.
- Hash each string with a 64-bit FNV-1a function. Start at slot
hash & mask. - Probe forward, wrapping around:
- If the slot is empty, store the string and its hash there and increment the distinct counter.
- If the slot holds the same hash and an equal string, the string is a duplicate, so stop without counting it.
- Otherwise, move to the next slot.
Why it is correct
A string is always looked up along the same probe sequence, starting from the same slot. Slots are never removed, so if a string was inserted earlier, the probe reaches it before reaching any empty slot, and it is recognized as a duplicate. If it was not inserted earlier, the probe reaches an empty slot (one always exists because the load factor is below 1) and the string is inserted exactly once. Comparing the full string, not just the hash, guarantees that hash collisions between different strings never cause a wrong merge. The counter therefore equals the number of distinct strings. The empty string hashes to the offset basis and is handled like any other value; storing it works because empty slots are marked with None, not with "".
Complexity
- Time: expected O(L) where L is the total number of characters, since hashing costs the string length and probing is expected O(1) at a load factor of at most 0.7. In the worst case, with many collisions, probing degrades toward O(n) per insert.
- Space: O(n) for the table, plus the stored references to the strings.
Reference solution
from typing import List
def count_unique_strings(strings: List[str]) -> int:
n = len(strings)
if n == 0:
return 0
# Capacity: power of two keeping load factor at or below 0.7
capacity = 8
while capacity * 7 < n * 10:
capacity <<= 1
slots = [None] * capacity
hashes = [0] * capacity
mask = capacity - 1
distinct = 0
def string_hash(value):
# 64-bit FNV-1a
h = 1469598103934665603
for ch in value:
h ^= ord(ch)
h = (h * 1099511628211) & 0xFFFFFFFFFFFFFFFF
return h
for value in strings:
h = string_hash(value)
index = h & mask
while True:
existing = slots[index]
if existing is None:
slots[index] = value
hashes[index] = h
distinct += 1
break
if hashes[index] == h and existing == value:
break
index = (index + 1) & mask
return distinct