Java Instance 9-Hamming distance Hamming Distance
http://blog.csdn.net/kindterry/article/details/6581344
/** in information theory, the Hamming distance between two equal-length strings
* is the number of different characters in the corresponding position of two strings,
* In other words, Hamming distance is the length of the character to replace a string with another string.
* For example, the Hamming distance between 1011101 and 1001001 is 2,
The Hamming distance between *toned and roses is 3.
* Hamming weight is the Hamming distance of a string of 0 strings relative to the same length,
* That is, it is the number of non-zero elements in the string: for a binary string, it is the number of 1,
* So the Hamming weight of 11101 is 4.
* The following code shows how Hamming distance and Hamming weight are calculated in Java.
*/
Package re;
public class Hammingdistance {
public static void Main (string[] args) {
String str1 = "ABCDEFG";
String str2 = "Aacceeg";
Hammingdistance HD = new Hammingdistance ();
int distance = Hd.getdistance (str1, str2);
System.out.println ("Distance is" + distance);
int weight = hd.getweight (255);
System.out.println ("Weight is" + weight);
}
/**
* Calculate Hamming Distance between, strings
*
* @author
* @param str1 the 1st string
* @param str2 the 2nd string
* @return Hamming Distance between STR1 and str2
*/
public int Getdistance (string str1, String str2) {
int distance;
if (str1.length () = Str2.length ()) {
Distance =-1;
} else {
Distance = 0;
for (int i = 0; i < str1.length (); i++) {
if (Str1.charat (i)! = Str2.charat (i)) {
distance++;
}
}
}
return distance;
}
/**
* Calculate Hamming weight for binary number
* @author
* @param i the binary number
* @return Hamming weight of the binary number
*/
public int getweight (int i) {
int n;
for (n = 0; i > 0; n++) {
I &= (i-1);
}
return n;
}
}
Similarity algorithm, java instance 9-Hamming distance Hamming Distance