# Hamming Distance between two strings

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 are different.

Examples:

Input : str1[] = "geeksforgeeks", str2[] = "geeksandgeeks" Output : 3 Explanation : The corresponding character mismatch are highlighted. "geeksgeeks" and "geeksforgeeks" Input : str1[] = "1011101", str2[] = "1001001" Output : 2 Explanation : The corresponding character mismatch are highlighted. "10and1101" and "1011001"0

This problem can be solved with a simple approach in which we traverse the strings and count the mismatch at corresponding position. Extended form of this problem is edit distance.

**Algorithm : **

int hammingDist(char str1[], char str2[]) { int i = 0, count = 0; while(str1[i]!='') { if (str1[i] != str2[i]) count++; i++; } return count; }

Below is the implementation of two strings.

## C++

`// C++ program to find hamming distance b/w ` `// two string ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to calculate Hamming distance ` `int` `hammingDist(` `char` `*str1, ` `char` `*str2) ` `{ ` ` ` `int` `i = 0, count = 0; ` ` ` `while` `(str1[i] != ` `''` `) ` ` ` `{ ` ` ` `if` `(str1[i] != str2[i]) ` ` ` `count++; ` ` ` `i++; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// driver code ` `int` `main() ` `{ ` ` ` `char` `str1[] = ` `"geekspractice"` `; ` ` ` `char` `str2[] = ` `"nerdspractise"` `; ` ` ` ` ` `// function call ` ` ` `cout << hammingDist (str1, str2); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find hamming distance ` `// b/w two string ` `class` `GFG ` `{ ` `// function to calculate Hamming distance ` `static` `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 code ` `public` `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. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find hamming ` `// distance b/w two string ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// function to calculate ` `// Hamming distance ` `static` `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 code ` `public` `static` `void` `Main () ` `{ ` ` ` `String str1 = ` `"geekspractice"` `; ` ` ` `String str2 = ` `"nerdspractise"` `; ` ` ` ` ` `// function call ` ` ` `Console.Write(hammingDist(str1, str2)); ` `} ` `} ` ` ` `// This code is contributed by nitin mittal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find hamming distance b/w ` `// two string ` ` ` `// function to calculate ` `// Hamming distance ` `function` `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. ` `?> ` |

*chevron_right*

*filter_none*

Output:

4

Time complexity : O(n)

**Note : **For Hamming distance of two binary numbers, we can simply return 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 contribute.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.

## Recommended Posts:

- A Program to check if strings are rotations of each other or not
- Check whether two strings are anagram of each other
- Print all interleavings of given two strings
- Interleaving of two given strings with no common characters
- Find if a string is interleaved of two other strings | DP-33
- Rearrange a string so that all same characters become d distance away
- Find if an array of strings can be chained to form a circle | Set 1
- Given two strings, find if first string is a subsequence of second
- Print all possible strings that can be made by placing spaces
- Find all distinct palindromic sub-strings of a given string
- Check if two given strings are isomorphic to each other
- Check if edit distance between two strings is one
- Find all strings formed from characters mapped to digits of a number
- All combinations of strings that can be used to dial a number
- Generate all binary strings from given pattern