Hash Tables
Hash tables are a corner stone of data structures for most problems, they are useful when there are unique keys with given values
Last updated
primes = [3, 5, 7, ...]
def hash(string):
value = 1
for ch in string:
value *= primes[ord(ch) - ord('a')]
return valuefreq = [0] * 26
# is the same as
freq = {}
for ch in s:
if ch not in freq:
freq[ch] = 0acc = 0
for ch in string:
mask = 1 << (ord(ch) - ord('a'))
if acc ^ mask < acc: # duplicate found
return False
acc |= mask