Hamming Distance between two strings
Last Updated :
13 Sep, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int hammingDist(string str1, string str2)
{
int i = 0, count = 0;
while (str1[i] != '\0' ) {
if (str1[i] != str2[i])
count++;
i++;
}
return count;
}
int main()
{
string str1 = "geekspractice" ;
string str2 = "nerdspractise" ;
cout << hammingDist(str1, str2);
return 0;
}
|
C
#include <stdio.h>
int hammingDist( char * str1, char * str2)
{
int i = 0, count = 0;
while (str1[i] != '\0' ) {
if (str1[i] != str2[i])
count++;
i++;
}
return count;
}
int main()
{
char str1[] = "geekspractice" ;
char str2[] = "nerdspractise" ;
printf ( "%d" , hammingDist(str1, str2));
return 0;
}
|
Java
class GFG {
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;
}
public static void main(String[] args)
{
String str1 = "geekspractice" ;
String str2 = "nerdspractise" ;
System.out.println(hammingDist(str1, str2));
}
}
|
Python3
def hammingDist(str1, str2):
i = 0
count = 0
while (i < len (str1)):
if (str1[i] ! = str2[i]):
count + = 1
i + = 1
return count
str1 = "geekspractice"
str2 = "nerdspractise"
print (hammingDist(str1, str2))
|
C#
using System;
class GFG {
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;
}
public static void Main ()
{
String str1 = "geekspractice" ;
String str2 = "nerdspractise" ;
Console.Write(hammingDist(str1, str2));
}
}
|
PHP
<?php
function hammingDist( $str1 , $str2 )
{
$i = 0; $count = 0;
while (isset( $str1 [ $i ]) != '' )
{
if ( $str1 [ $i ] != $str2 [ $i ])
$count ++;
$i ++;
}
return $count ;
}
$str1 = "geekspractice" ;
$str2 = "nerdspractise" ;
echo hammingDist ( $str1 , $str2 );
?>
|
Javascript
<script>
function hammingDist(str1, str2)
{
let i = 0, count = 0;
while (i < str1.length)
{
if (str1[i] != str2[i])
count++;
i++;
}
return count;
}
let str1 = "geekspractice" ;
let str2 = "nerdspractise" ;
document.write(hammingDist (str1, str2));
</script>
|
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.
Share your thoughts in the comments
Please Login to comment...