![]() |
VOOZH | about |
You are given two strings of equal length, you have to find the Hamming Distance between these string.
Where the Hamming distance between two strings of equal length is the number of positions at which the corresponding character is different.
Examples:
Input : str1[] = "geeksforgeeks", str2[] = "geeksandgeeks" Output : 3 Explanation : The corresponding character mismatch are highlighted. "geeksgeeks" and "geeksgeeks" Input : str1[] = "1011101", str2[] = "1001001" Output : 2 Explanation : The corresponding character mismatch are highlighted. "10101" and "10101"
This problem can be solved with a simple approach in which we traverse the strings and count the mismatch at the corresponding position. The extended form of this problem is edit distance.
Algorithm :
int hammingDist(char str1[], char str2[])
{
int i = 0, count = 0;
while(str1[i]!='\0')
{
if (str1[i] != str2[i])
count++;
i++;
}
return count;
}
Below is the implementation of two strings.
4
Time complexity: O(n)
Note: For Hamming distance of two binary numbers, we can simply return a count of set bits in XOR of two numbers.