Hanming Distance gives an idea about the minimum number of edits required to transform one string to another. Note that for Hamming distance to work the strings must be of equal length.
For example,
Hamming Distance ( sun , son) = 1
Hamming Distance ( sun , sun) = 0
The code
______________________________________________________________________________
import thundering.typhoons.distance.Hamming;
public class DistanceDemo {
static String str1 = "sun";
static String str2 = "son";
static String str3 = "moon";
public static void main(String[] args) {
int distance = Hamming.getHammingDistance(str1, str1);
if (distance != -1) {
System.out.println("Hamming distance between "+str1+" and "+str1+" : "+distance);
}else{
System.out.println("Cannot compute the hamming distance betwen "+str1+" and "+str1);
}
distance = Hamming.getHammingDistance(str1, str2);
if (distance != -1) {
System.out.println("Hamming distance between "+str1+" and "+str2+" : "+distance);
}else{
System.out.println("Cannot compute the hamming distance betwen "+str1+" and "+str2);
}
distance = Hamming.getHammingDistance(str1, str3);
if (distance != -1) {
System.out.println("Hamming distance between "+str1+" and "+str3+" : "+distance);
}else{
System.out.println("Cannot compute the hamming distance betwen "+str1+" and "+str3);
}
}
}
package thundering.typhoons.distance;
public class Hamming {
public static int getHammingDistance(String str1, String str2) {
if (str1 == null || str2 == null) {
return Constants.CANNOT_COMPARE;
}
if (str1.length() != str2.length()) {
return Constants.CANNOT_COMPARE;
}
int distance = 0;
for (int i = 0; i < str1.length(); i++) {
if (str1.charAt(i) != str2.charAt(i)) {
distance++;
}
}
return distance;
}
}
______________________________________________________________________________
output:
Hamming distance between sun and sun : 0
Hamming distance between sun and son : 1
Cannot compute the hamming distance betwen sun and moon
For example,
Hamming Distance ( sun , son) = 1
Hamming Distance ( sun , sun) = 0
The code
______________________________________________________________________________
import thundering.typhoons.distance.Hamming;
public class DistanceDemo {
static String str1 = "sun";
static String str2 = "son";
static String str3 = "moon";
public static void main(String[] args) {
int distance = Hamming.getHammingDistance(str1, str1);
if (distance != -1) {
System.out.println("Hamming distance between "+str1+" and "+str1+" : "+distance);
}else{
System.out.println("Cannot compute the hamming distance betwen "+str1+" and "+str1);
}
distance = Hamming.getHammingDistance(str1, str2);
if (distance != -1) {
System.out.println("Hamming distance between "+str1+" and "+str2+" : "+distance);
}else{
System.out.println("Cannot compute the hamming distance betwen "+str1+" and "+str2);
}
distance = Hamming.getHammingDistance(str1, str3);
if (distance != -1) {
System.out.println("Hamming distance between "+str1+" and "+str3+" : "+distance);
}else{
System.out.println("Cannot compute the hamming distance betwen "+str1+" and "+str3);
}
}
}
package thundering.typhoons.distance;
public class Hamming {
public static int getHammingDistance(String str1, String str2) {
if (str1 == null || str2 == null) {
return Constants.CANNOT_COMPARE;
}
if (str1.length() != str2.length()) {
return Constants.CANNOT_COMPARE;
}
int distance = 0;
for (int i = 0; i < str1.length(); i++) {
if (str1.charAt(i) != str2.charAt(i)) {
distance++;
}
}
return distance;
}
}
______________________________________________________________________________
output:
Hamming distance between sun and sun : 0
Hamming distance between sun and son : 1
Cannot compute the hamming distance betwen sun and moon
No comments:
Post a Comment