This algorithm calculates the distance between two strings, however they have to be of equal length.

It measures the minimum number of substitutions for the two strings to be equal.

It is used in telecommunication (also know as signal distance), it is also used in systematics as a measure of genetic distance.

Wikipedia entry can be found here:

public class Hamming
{
private String compOne;
private String compTwo;
public Hamming(String one, String two)
{
compOne = one;
compTwo = two;
}
///
// Calculating the Hamming Distance for two strings requires the string to be of the same length.
///
public int getHammingDistance()
{
if (compOne.length() != compTwo.length())
{
return -1;
}
int counter = 0;
for (int i = 0; i < compOne.length(); i++)
{
if (compOne.charAt(i) != compTwo.charAt(i)) counter++;
}
return counter;
}
///
// Hamming distance works best with binary comparisons, this function takes a string arrary of binary
// values and returns the minimum distance value
///
public int minDistance(String[] numbers)
{
int minDistance = Integer.MAX_VALUE;
if (checkConstraints(numbers))
{
for (int i = 1; i < numbers.length; i++)
{
int counter = 0;
for (int j = 1; j <= numbers[i].length(); j++)
{
if (numbers[i-1].charAt(j-1) != numbers[i].charAt(j-1))
{
counter++;
}
}
if (counter == 0) return counter;
if (counter < minDistance) minDistance = counter;
}
}
else
{
return -1;
}
return minDistance;
}
private Boolean checkConstraints(String[] numbers)
{
if (numbers.length > 1 && numbers.length <=50)
{
int prevLength = -1;
for (int i = 0; i < numbers.length; i++)
{
if (numbers[i].length() > 0 && numbers[i].length() <= 50)
{
if (prevLength == -1)
{
prevLength = numbers[i].length();
}
else
{
if (prevLength != numbers[i].length())
{
return false;
}
}
for (int j = 0; j < numbers[i].length(); j++)
{
if (numbers[i].charAt(j) != '0' && numbers[i].charAt(j) != '1')
{
return false;
}
}
}
else
{
return false;
}
}
}
else
{
return false;
}
return true;
}
}

### Like this:

Like Loading...

*Related*

Tags: algorithm, distance, Hamming, Java

This entry was posted on June 28, 2010 at 09:20 and is filed under Algorithm, Data, Hamming, Java Example. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

June 28, 2010 at 09:23 |

[…] Damerau-Levenshtein Hamming Jaro Winkler N-Gram Markov […]

February 18, 2012 at 23:24 |

Hi! Thanks for the post.

For the function minDistance(), il really dont understand 😦 you can give me an exemple how using it?

many thanks!

March 18, 2013 at 11:01 |

thank you so much. by the way, what about a java code to compute eucledian distance?