Tutorials › Topology (Undergraduate) › Hamming Distance on Finite Strings

Metric Spaces and Open Balls · Tutorial 42 of 799

Hamming Distance on Finite Strings

Learn to compute Hamming distances between equal-length strings, verify the metric axioms, and use the distance to describe nearby strings.

Beginner 9 min read

What You'll Learn

  • Define Hamming distance for strings of a fixed length over an alphabet.
  • Prove the triangle inequality by comparing sets of mismatched coordinates.
  • Verify all four metric axioms for Hamming distance.
  • Compute distances between binary strings and strings over other alphabets.
  • Describe which distances can occur, including the one-symbol alphabet case.
  • Use the integer-valued distance to identify a metric open ball.

Counting Coordinate Mismatches

When two strings have the same length, one way to measure their difference is to count the positions where their symbols disagree. This count ignores how different the symbols themselves may be: changing one symbol to another contributes one mismatch, just as any other single-coordinate change does. The resulting distance is called the Hamming distance. It is useful for comparing finite strings and for describing how many coordinate changes separate one string from another.

Fix a nonempty alphabet \(\Sigma\) and a positive integer \(n\). A string of length \(n\) over \(\Sigma\) is an element \(x=(x_1,\ldots,x_n)\) of \(\Sigma^n\), where each coordinate \(x_i\) belongs to \(\Sigma\). We compare strings of the same length; the Hamming distance as defined here does not compare strings of different lengths.

Definition: For \(x,y\in\Sigma^n\), the Hamming distance from \(x\) to \(y\) is \(d_H(x,y)=|\{i\in\{1,\ldots,n\}:x_i\ne y_i\}|\). Thus \(d_H(x,y)\) is the number of coordinates at which the strings differ.

The value is an integer between \(0\) and \(n\), inclusive. It is zero precisely when the strings agree at every coordinate. For a binary alphabet, \(\Sigma=\{0,1\}\), each coordinate contributes either zero or one to the count. The same definition applies to any nonempty alphabet, not just a binary one.

Why Hamming Distance Is a Metric

A metric on a set \(X\) is a function \(d:X\times X\to[0,\infty)\) satisfying four conditions: nonnegativity, identity of indiscernibles, symmetry, and the triangle inequality. Identity of indiscernibles means \(d(x,y)=0\) if and only if \(x=y\). The following result verifies these conditions for Hamming distance.

Theorem: For every nonempty alphabet \(\Sigma\) and every positive integer \(n\), \(d_H\) is a metric on \(\Sigma^n\).

Proof. Let \(x,y,z\in\Sigma^n\). The number of mismatching coordinates is a cardinality, so \(d_H(x,y)\ge0\). If \(d_H(x,y)=0\), there is no coordinate at which \(x_i\ne y_i\). Therefore \(x_i=y_i\) for every \(i\), which means \(x=y\). Conversely, if \(x=y\), there are no mismatching coordinates and \(d_H(x,y)=0\).

For every coordinate \(i\), \(x_i\ne y_i\) holds exactly when \(y_i\ne x_i\), so the mismatch sets for \((x,y)\) and \((y,x)\) are equal. Hence \(d_H(x,y)=d_H(y,x)\).

It remains to prove the triangle inequality. Define \(M(x,z)=\{i:x_i\ne z_i\}\), and define \(M(x,y)\) and \(M(y,z)\) in the same way. If \(i\in M(x,z)\), then \(x_i\ne z_i\). It cannot be true both that \(x_i=y_i\) and that \(y_i=z_i\), because those equalities would imply \(x_i=z_i\). Thus \(i\in M(x,y)\) or \(i\in M(y,z)\). We have shown \(M(x,z)\subseteq M(x,y)\cup M(y,z)\). Taking cardinalities and using that the size of a union is at most the sum of the sizes gives \(d_H(x,z)\le d_H(x,y)+d_H(y,z)\). All four metric conditions hold, so \(d_H\) is a metric. \(\square\)

The triangle inequality has a coordinate-level explanation: every position where the first and third strings disagree must register a disagreement in at least one of the two comparisons that pass through the middle string. A coordinate may contribute to both comparisons on the right, but it cannot be missing from both.

Worked Example: Three Binary Strings

Let \(x=01010\), \(y=01111\), and \(z=00001\). Compute the three pairwise distances and check the triangle inequality for this triple.

Compare \(x\) with \(y\) coordinate by coordinate. Their entries are \(0=0\), \(1=1\), \(0\ne1\), \(1=1\), and \(0\ne1\). They differ at coordinates \(3\) and \(5\), so \(d_H(x,y)=2\).

For \(y\) and \(z\), the entries are \(0=0\), \(1\ne0\), \(1\ne0\), \(1\ne0\), and \(1=1\). The mismatch coordinates are \(2,3,4\), so \(d_H(y,z)=3\).

For \(x\) and \(z\), the entries are \(0=0\), \(1\ne0\), \(0=0\), \(1\ne0\), and \(0\ne1\). The mismatch coordinates are \(2,4,5\), so \(d_H(x,z)=3\).

Check: the triangle inequality for this ordered triple reads \(d_H(x,z)\le d_H(x,y)+d_H(y,z)\). Substitution gives \(3\le2+3=5\), which is true. The mismatch set \(\{2,4,5\}\) is also contained in \(\{3,5\}\cup\{2,3,4\}=\{2,3,4,5\}\), as the proof predicts.

Computing Distances Coordinate by Coordinate

A reliable calculation records the coordinates first and counts the disagreements second. This avoids confusing the location of a mismatch with the value of the distance. For strings with symbols other than \(0\) and \(1\), the rule is unchanged: equality at a coordinate contributes zero, and inequality contributes one.

Worked Example: Strings Over a Three-Symbol Alphabet

Use the alphabet \(\Sigma=\{a,b,c\}\), and let \(u=(a,b,c,a)\) and \(v=(a,c,b,a)\). Find their Hamming distance.

At coordinate \(1\), \(u_1=a=v_1\), so this coordinate contributes zero. At coordinate \(2\), \(u_2=b\) and \(v_2=c\), so it contributes one. At coordinate \(3\), \(u_3=c\) and \(v_3=b\), so it contributes one. At coordinate \(4\), \(u_4=a=v_4\), so it contributes zero. The mismatch set is \(\{2,3\}\), and therefore \(d_H(u,v)=|\{2,3\}|=2\).

Check: there are four coordinates in total, and the coordinate comparison identified exactly two equalities and two inequalities. The count is \(0+1+1+0=2\), agreeing with the mismatch-set calculation.

Because the distance is integer-valued, it is sometimes helpful to describe strings by how many coordinates may differ from a fixed string. In the topology induced by a metric, an open ball of radius \(r>0\) about \(x\) is the set \(B_{d_H}(x,r)=\{y:d_H(x,y)<r\}\). The strict inequality matters: at radius \(2\), for example, strings at distance exactly \(2\) are not in the ball.

Worked Example: A Ball in Binary Strings of Length Three

Take \(\Sigma=\{0,1\}\), \(n=3\), center \(x=000\), and radius \(r=2\). Determine \(B_{d_H}(000,2)\).

A string belongs to this ball precisely when its distance from \(000\) is strictly less than \(2\). Since distances are nonnegative integers, the allowed distances are \(0\) and \(1\). The string \(000\) has distance \(0\). The strings \(100\), \(010\), and \(001\) each differ from \(000\) at exactly one coordinate, so each has distance \(1\). The remaining binary strings of length three are \(110\), \(101\), \(011\), and \(111\); the first three differ at two coordinates, and \(111\) differs at three, so none is in the ball.

Consequently \(B_{d_H}(000,2)=\{000,100,010,001\}\). Check: the distances of the listed strings are \(0,1,1,1\), all strictly less than \(2\). For instance, \(d_H(110,000)=2\), so \(110\) is excluded by the strict inequality.

Which Distance Values Can Occur?

Proposition: For \(n\ge1\), every Hamming distance in \(\Sigma^n\) is an integer from \(0\) through \(n\). If \(\Sigma\) contains at least two symbols, every integer in that range occurs as the distance between some pair of strings. If \(\Sigma\) has exactly one symbol, the only distance that occurs is \(0\).

Proof. A mismatch set is a subset of \(\{1,\ldots,n\}\), so its cardinality is an integer between \(0\) and \(n\). This proves the first assertion. If \(\Sigma\) contains at least two symbols, choose distinct symbols \(\alpha,\beta\in\Sigma\). For any integer \(k\) with \(0\le k\le n\), let \(x\) have symbol \(\alpha\) in every coordinate. Let \(y\) have symbol \(\beta\) in its first \(k\) coordinates and symbol \(\alpha\) in its other \(n-k\) coordinates. Then \(x\) and \(y\) differ exactly in coordinates \(1,\ldots,k\), so \(d_H(x,y)=k\). This also covers \(k=0\), when there are no differing coordinates, and \(k=n\), when all coordinates differ. If \(\Sigma\) has exactly one symbol, every string in \(\Sigma^n\) has that symbol at every coordinate. Any two such strings are equal, so every distance is \(0\). \(\square\)

In particular, the maximum possible distance is \(n\) when the alphabet has at least two symbols, but this need not hold for every alphabet. For a one-symbol alphabet, no pair differs at any coordinate. This is a useful reminder that the length gives an upper bound on distance; it does not by itself guarantee that the upper bound is attained.

A Common Counting Pitfall

Common mistake. When comparing \(x=01010\) and \(z=00001\), it is incorrect to report mismatches at coordinates \(1\) and \(5\), and then conclude that the distance is \(2\). Check the entries in order: coordinate \(1\) is \(0=0\); coordinate \(2\) is \(1\ne0\); coordinate \(3\) is \(0=0\); coordinate \(4\) is \(1\ne0\); and coordinate \(5\) is \(0\ne1\). The correct mismatch set is \(\{2,4,5\}\), so \(d_H(x,z)=3\). Rechecking each coordinate is especially important when using a computed distance in a triangle-inequality check.

Hamming distance gives a simple metric structure to finite strings: it satisfies the metric axioms, counts symbol changes one coordinate at a time, and makes the integer nature of distances explicit. In applications such as comparing transmitted and received strings, the distance records the number of positions that disagree. The metric itself does not say why two strings differ; it supplies a precise, verifiable measure of how many coordinate positions separate them.

Check Your Understanding

For each question, count coordinate mismatches directly and keep the fixed string length in view.

  1. Compute \(d_H(10101,11100)\) and list the coordinates at which the strings differ.
  2. For strings of length \(4\) over \(\{a,b\}\), give two strings whose Hamming distance is \(4\).
  3. Explain why Hamming distance between strings of different lengths has not been defined here.
  4. For the one-symbol alphabet \(\Sigma=\{a\}\) and \(n=3\), what distances occur in \(\Sigma^n\)?
  5. In \(\{0,1\}^4\), describe which strings lie in \(B_{d_H}(0000,1)\), paying attention to the strict inequality in the definition of an open ball.