I have a private class that I want to be able to find the shortest Hamming Distance between two Strings of equal length in Java. The private class holds a char[]
and contains a method to compare against other char arrays. It returns true if there is only a hamming distance of one.
The average length of the Strings used is 9 characters. They range from 1 to 24 characters.
Is there a way to make the isDistanceOne(char[])
method go any faster?
private class WordArray {
char[] word;
/**
* Creates a new WordArray object.
*
* @param word The word to add to the WordArray.
*/
private WordArray(String word) {
this.word = word.toCharArray();
}
/**
* Returns whether the argument is within a Hamming Distance of one from
* the char[] contained in the WordArray.
*
* Both char[]s should be of the same length.
*
* @param otherWord The word to compare with this.word.
* @return boolean.
*/
private boolean isDistanceOne(char[] otherWord) {
int count = 0;
for(int i = 0; i < otherWord.length; i++) {
if (this.word[i] != otherWord[i])
count++;
}
return (count == 1);
}
}