Sub topic 3
A Simpler Cousin: Hamming Distance for Equal-Length Strings
This chapter assumes you know the three edit operations from the earlier chapters (insertion, deletion, substitution) and the definition of Levenshtein distance. You should also be able to read basic Python and Big-O notation.
What Hamming Distance Measures
Hamming distance is the number of positions at which two strings of equal length have different characters. It allows only one operation, substitution. Insertions and deletions are not allowed, so characters cannot shift position. Position 0 is always compared with position 0, position 1 with position 1, and so on.
This restriction makes the measure very simple. Hamming distance needs no table and no recurrence, only one pass over the two strings.
A Worked Example
Compare karolin with kathrin one position at a time:
| Position | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| `karolin` | k | a | r | o | l | i | n |
| `kathrin` | k | a | t | h | r | i | n |
| Differs? | no | no | yes | yes | yes | no | no |
Three positions differ, so the Hamming distance is 3.
Implementation
def hamming(a, b):
if len(a) != len(b):
raise ValueError("Hamming distance requires equal-length strings")
return sum(x != y for x, y in zip(a, b))
assert hamming("karolin", "kathrin") == 3
assert hamming("1011101", "1001001") == 2
print(hamming("karolin", "kathrin"))The function runs in O(n) time and O(1) extra space. Levenshtein distance needs O(mn) time with the standard table.
How It Relates to Levenshtein Distance
For equal-length strings, every Hamming alignment is also a valid Levenshtein edit sequence: substitute at each position that differs. Levenshtein looks for the cheapest sequence among all possible ones, so it can never cost more:
- Levenshtein(a, b) ≤ Hamming(a, b) whenever len(a) == len(b).
The two can be far apart. Take abcd and bcda. Every position differs, so the Hamming distance is 4. Levenshtein distance is 2: delete the leading a, then insert a at the end. Hamming distance cannot account for a shift, because it has no insertion or deletion to realign the strings.
When Hamming Distance Is the Right Choice
Use Hamming distance when position itself carries meaning and the lengths are fixed:
- Error-detecting and error-correcting codes, where a fixed-width codeword has some bits flipped.
- Comparing hashes or binary fingerprints, often computed as
bin(x ^ y).count("1")on integers. - DNA sequences aligned in advance, where only point mutations matter.
Typed text is a different case. Typos often add or drop a letter, which shifts everything after it, so Hamming distance gives misleading results there.
Common Errors
- Applying Hamming distance to strings of different lengths, either by padding or by letting
ziptruncate. - Assuming Hamming and Levenshtein distances are equal for equal-length strings. They are equal only when the best alignment involves no shifting.
- Comparing Unicode strings before normalizing them. A character such as
écan be stored as one code point or as two, which changes the lengths and the positions.
Exercise
What is the Hamming distance between "10110" and "11100"?
Show answer
2. The strings differ at position 1 (0 vs 1) and at position 3 (1 vs 0).
Exercise
Compute the Hamming and Levenshtein distances between "flow" and "lowf", and explain why they differ.
Show answer
Hamming is 4, because every position differs (f/l, l/o, o/w, w/f). Levenshtein is 2: delete the leading "f" and insert "f" at the end. Hamming only allows substitutions, so it cannot realign the shifted characters.