logo

Hamming Distance entre deux cordes

On vous donne deux chaînes de longueur égale, vous devez trouver la Distance de Hamming entre ces chaînes. 
Où la distance de Hamming entre deux chaînes de longueur égale est le nombre de positions auxquelles le caractère correspondant est différent. 

Exemples : 

Input : str1[] = 'geeksforgeeks' str2[] = 'geeksandgeeks' Output : 3 Explanation : The corresponding character mismatch are highlighted. 'geeks  for  geeks' and 'geeks  and  geeks' Input : str1[] = '1011101' str2[] = '1001001' Output : 2 Explanation : The corresponding character mismatch are highlighted. '10  1  1  1  01' and '10  0  1  0  01'

Ce problème peut être résolu avec une approche simple dans laquelle nous parcourons les chaînes et comptons la disparité à la position correspondante. La forme étendue de ce problème est modifier la distance.



Algorithme : 

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

Vous trouverez ci-dessous l'implémentation de deux chaînes. 

C++
// C++ program to find hamming distance b/w two string #include    using namespace std; // function to calculate Hamming distance int hammingDist(string str1 string str2) {  int i = 0 count = 0;  while (str1[i] != '') {  if (str1[i] != str2[i])  count++;  i++;  }  return count; } // driver code int main() {  string str1 = 'geekspractice';  string str2 = 'nerdspractise';  // function call  cout << hammingDist(str1 str2);  return 0; } // This code is contributed by Sania Kumari Gupta (kriSania804) 
C
// C program to find hamming distance b/w two string #include  // 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  printf('%d' hammingDist(str1 str2));  return 0; } // This code is contributed by Sania Kumari Gupta // (kriSania804) 
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 Sania Kumari Gupta // (kriSania804) 
Python3
# Python3 program to find  # hamming distance b/w two  # string  # Function to calculate # Hamming distance  def 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 call  print(hammingDist(str1 str2)) # This code is contributed by avanitrachhadiya2155 
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 
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. ?> 
JavaScript
<script> // JavaScript program to find hamming distance b/w // two string // function to calculate Hamming distance function 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> 

Sortir
4

Complexité temporelle : O(n)

Note: Pour la distance de Hamming de deux nombres binaires, nous pouvons simplement renvoyer un nombre de bits définis dans XOR de deux nombres.

Créer un quiz