Skip to main content

Hamming Distance

Definition​

The Hamming Distance Algorithm calculates the number of positions at which corresponding symbols differ between two strings of equal length

Practice​

hammingDistance(string1, string2):
if length(string1) ≠ length(string2):
throw "Error: Strings must be of equal length"

distance = 0
for i = 0 to length(string1) - 1:
if string1[i] ≠ string2[i]:
distance += 1

return distance