Hamming Distance between two strings - GeeksforGeeks

19 May, 2021
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.
"geeksforgeeks" and "geeksandgeeks"

Input : str1[] = "1011101", str2[] = "1001001"
Output : 2
Explanation : The corresponding character mismatch are highlighted.
"1011101" and "1001001"
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.  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find hamming distance b/w// two string#include<bits/stdc++.h>using namespace std; // function to calculate Hamming distanceint hammingDist(char *str1, char *str2){    int i = 0, count = 0;    while (str1[i] != '\0')    {        if (str1[i] != str2[i])            count++;        i++;    }    return count;} // driver codeint main(){    char str1[] = "geekspractice";    char str2[] = "nerdspractise";     // function call    cout << hammingDist (str1, str2);     return 0;}
// Java program to find hamming distance// b/w two stringclass GFG{// function to calculate Hamming distancestatic int hammingDist(String str1, String str2){    int i = 0, count = 0;    while (i < str1.length())    {        if (str1.charAt(i) != str2.charAt(i))            count++;        i++;    }    return count;} // Driver codepublic static void main (String[] args){    String str1 = "geekspractice";    String str2 = "nerdspractise";     // function call    System.out.println(hammingDist (str1, str2));}} // This code is contributed by Anant Agarwal.
# Python3 program to find# hamming distance b/w two# string # Function to calculate# Hamming distancedef hammingDist(str1, str2):    i = 0    count = 0     while(i < len(str1)):        if(str1[i] != str2[i]):            count += 1        i += 1    return count # Driver code str1 = "geekspractice"str2 = "nerdspractise" # function callprint(hammingDist(str1, str2)) # This code is contributed by avanitrachhadiya2155
// C# program to find hamming// distance b/w two stringusing System; class GFG {     // function to calculate// Hamming distancestatic int hammingDist(String str1,                       String str2){    int i = 0, count = 0;    while (i < str1.Length)    {        if (str1[i] != str2[i])            count++;        i++;    }    return count;} // Driver codepublic static void Main (){    String str1 = "geekspractice";    String str2 = "nerdspractise";     // function call    Console.Write(hammingDist(str1, str2));}} // This code is contributed by nitin mittal
<?php// PHP program to find hamming distance b/w// two string // function to calculate// Hamming distancefunction hammingDist($str1, $str2){    $i = 0; $count = 0;    while (isset($str1[$i]) != '')    {        if ($str1[$i] != $str2[$i])            $count++;        $i++;    }    return $count;}     // Driver Code    $str1 = "geekspractice";    $str2 = "nerdspractise";     // function call    echo hammingDist ($str1, $str2);     // This code is contributed by nitin mittal.?>
<script>// JavaScript program to find hamming distance b/w// two string // function to calculate Hamming distancefunction hammingDist(str1, str2){    let i = 0, count = 0;    while (i < str1.length)    {        if (str1[i] != str2[i])            count++;        i++;    }    return count;} // driver code    let str1 = "geekspractice";    let str2 = "nerdspractise";     // function call    document.write(hammingDist (str1, str2));   // This code is contributed by Manoj.</script>
Output:  
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.This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
avanitrachhadiya2155
mank1083
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not
KMP Algorithm for Pattern Searching
Array of Strings in C++ (5 Different Ways to Create)
Different methods to reverse a string in C/C++
Convert string to char array in C++
Longest Palindromic Substring | Set 1
Caesar Cipher in Cryptography
Top 50 String Coding Problems for Interviews
Length of the longest substring without repeating characters