Beginners Programs :

Java Program to Find Hamming Distance Two Strings | Java Programs

Java program to calculate hamming distance between two strings & two Integers. The following program has been written in multiple ways along with sample output. If you looking out for any clarficiations regarding hamming distance, leave a comment here.

For solving any problem, we first need to understand the problem statement properly and then, look for any constraints if mentioned.

Then, we need to determined our required inputs to solve the problem along with the expected output of our problem. Later, we find the logic to be used to arrive at the expected output by making use of the inputs.

Our problem statement here is, to find the hamming distance between two strings. Hamming distance is the comparison between two strings of equal length to find the number of positions in which they differ from each other.

For this, our required input would be two strings and the resultant output will be an integer denoting the number of positions they differ or hamming distance of the two strings.

To read the two input strings at runtime, we can make use of Scanner class.

This enables us to read input of any primitive datatype at runtime from console screen. We need to first create object of this Scanner class and since our input is string, we invoke the nextLine() method of the Scanner class to read inputs. This is done as follows:

After gathering inputs, we call the hammingdistance() method and send the two input strings (s1 and s2) as parameters or argument.

In this method, we first check whether the two strings have same length or not. If they are not same, we return -1 to the main method.

if(s1.length()!=s2.length()) {

return -1;

}

When both the lengths are same, we continue the further steps. We iterate until we reach the end of both the strings and compare every individual character of both the string with same index positions.

If both the characters placed at the index are equal nothing happens. But, if the are not equal then we increment the count variable by 1.

for(int i=0;i<s1.length();i++) {

if(s1.charAt(i)!=s2.charAt(i))

count++;

}

When we reach the end of string, we come out of the loop. The resultant value of the count variable at the end of iteration is nothing but, our resultant output hamming code.

This count variable represents the number of positions which have different values in the two strings or the number of positions to be changed so that the two strings become equal. This count variable is then returned to out main method.

This returned value is stored in another variable (res). If this variable is not negative then, we display the hamming distance (res) in our console screen by making use of println() method.

if(res>=0) {

System.out.print(“HammingDistance(“+s1+”,”+s2+”)———->”+res);

}

Else, if the value is negative (-1) then, it means the length of two string is not equal. So, we display the same in the console screen.

else {

System.out.print(“enter same length string”);

}

Java Program To Find Hamming Distance

Output:

Output – 2: