Compare two strings
Both strings must have exactly the same number of characters. Spaces and punctuation count as characters.
Example Hamming distances
1011101
1001001
Distance: 2
karolin
kathrin
Distance: 3
GATTACA
GACTATA
Distance: 2
110011
110011
Distance: 0
What is Hamming distance?
The Hamming distance between two equal-length strings is the number of positions where their corresponding symbols differ. It is commonly used in coding theory, digital communications, error detection, bioinformatics, and string comparison.
Formula
For strings \(A\) and \(B\) of length \(n\), the Hamming distance is:
For example, 1011101 and 1001001 differ at positions 3 and 5, so their Hamming distance is 2.
Hamming distance vs. similarity
This calculator also reports positional similarity, calculated as:
For non-empty strings, a distance of 0 means all positions match. A distance equal to the string length means every position differs. Similarity is a simple positional measure, not a guarantee of semantic similarity.
Important limitations
- Strings must have equal lengths. Insertions and deletions require a different measure, such as Levenshtein distance.
- For binary data, compare bit strings of equal length.
- Text comparison uses Unicode characters if the PHP `mbstring` extension is installed; without it, the fallback compares UTF-8 bytes, which may not match user-perceived characters.
- Ignoring case changes the strings before comparison. It does not ignore spaces or punctuation.
Frequently asked questions
Can I calculate Hamming distance for binary strings?
Yes. Enter two binary strings of equal length. The distance counts the bit positions that differ.
Why must both strings have equal length?
Hamming distance compares corresponding positions. When strings have different lengths, there is no direct one-to-one positional comparison for every character.
Does a Hamming distance of zero mean the strings are identical?
Yes, after applying the selected case-sensitivity setting. A distance of zero means every compared position matches.
Can I compare DNA sequences?
Yes. Paste two equal-length sequences such as DNA strings. The calculator compares the symbols at each position and counts mismatches.
What is the difference between Hamming and Levenshtein distance?
Hamming distance counts substitutions at corresponding positions and requires equal lengths. Levenshtein distance allows insertions, deletions, and substitutions, so it can compare strings of different lengths.