Calculate the Hamming distance between two equal-length strings.Hamming distance is the number of differing positions (for equal-length strings only).
Last updated: March 2026 | By Patchworkr Team
Hamming distance, named after Richard Hamming, is a metric that measures the difference between two strings of equal length. It counts the minimum number of substitutions required to change one string into another, or equivalently, the number of positions at which the corresponding symbols differ.
Originally developed for error detection and correction in telecommunications, Hamming distance is now fundamental in information theory, coding theory, cryptography, and bioinformatics. It's particularly useful for comparing binary strings, DNA sequences, error-correcting codes, and data transmission verification.
The concept is simple but powerful: if two strings have a Hamming distance of 0, they are identical. A distance of 1 means they differ by exactly one character. The larger the distance, the more different the strings are. This makes it an essential tool for detecting errors in digital communication and storage systems.
Calculating Hamming distance is straightforward:
Hamming distance is used in various fields:
Let's calculate the Hamming distance between "10110" and "10011":
Hamming distance is only defined for strings of equal length. For strings of different lengths, you would need to use a different metric like Levenshtein distance, which allows insertions and deletions.
Yes, Hamming distance treats uppercase and lowercase letters as different characters. 'A' and 'a' would count as a difference. Convert strings to the same case first if you want case-insensitive comparison.
Normalized Hamming distance is the Hamming distance divided by the length of the strings, giving a value between 0 and 1. This makes it easier to compare distances between strings of different lengths in different contexts.
Yes, Hamming distance is widely used in bioinformatics to compare DNA or protein sequences of equal length. It measures point mutations (single nucleotide polymorphisms) between sequences, making it valuable for evolutionary studies.
Hamming distance and Hamming code are related concepts. Hamming code is designed to have a minimum Hamming distance between valid codewords, which determines its error detection and correction capabilities.
Yes, a Hamming distance of 0 means the two strings are identical - there are no positions where they differ. This is the minimum possible Hamming distance.
The maximum Hamming distance equals the length of the strings, which occurs when every position differs. For example, '111' and '000' have a Hamming distance of 3.
Hamming distance only counts substitutions and requires equal-length strings. Edit distance (Levenshtein) also counts insertions and deletions, works with any string lengths, and generally gives a smaller or equal distance.
Related Tools