Question

In: Computer Science

Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2...

Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2 of the same length n. What is the complexity of your algorithm?

Solutions

Expert Solution

Algo-

function Find_Hamming_distance(string s1, string s2)

Declare 3 variable i,n,counter=0 where n will store length of string,counter varible will store number of position where character mismatch and i will udes as index.

run while loop till n>0, where n is length of string

starting from index 0,check if at same index in both sring,character are different, then increase counter =counter+1. Move index one position ahead after each iteration.

After the end of while loop, return counter.

Pseudocode-

Find_Hamming_distance(string s1, string s2)

{

      counter=0 //store hamming distance

      n=length of any string

      while(n--)

      {

       if(s1[i]!=s2[i])

       {

        counter=counter+1

        }

       i=i+1

     }

return counter;

}

Complexity of algorithm is O(n) where n is length of string.

Complete code in c++

#include<bits/stdc++.h>
using namespace std;

int Find_Hamming_distance(string s1,string s2)
{
int i = 0, counter=0; //counter will store hamming distance
int n=s1.length();
while (n--)
{
if (s1[i] != s2[i]) //check if character are not same then increment counter variable by 1
counter++;
i++;
}
return counter;
}

int main()
{
string s1 = "checkprice";
string s2 = "shackprise";

cout << Find_Hamming_distance (s1, s2);

return 0;
}

output


Related Solutions

Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2...
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2 of the same length n. What is the complexity of your algorithm?
In python. Write a program that takes 2 string inputs and calculates the Hamming Distance. Hamming...
In python. Write a program that takes 2 string inputs and calculates the Hamming Distance. Hamming distance between two strings is the number of positions at which the corresponding symbols are different. The program should output an integer representing this distance. For example a = XXWWZZ b = ZZWWXX answer = 4 More examples: "Phone" and "PHOONE" = 3 "God" and "Dog" = 2 "Dog" and "House" = 4
Given the two stacks S1 and S2 each with a set of numbers, write an algorithm...
Given the two stacks S1 and S2 each with a set of numbers, write an algorithm to check if the two stacks are identical (have the same information). Use only the Push and Pop operations. The original stacks should be kept. Briefly explain the time complexity of your algorithm.
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
If there are two energy states, S1 and S2 respectively such that S2>S1 then there exists...
If there are two energy states, S1 and S2 respectively such that S2>S1 then there exists some probability for an atom in S2 to decay to S1. What actually causes the atom to decay to the lower energy state? is it the fact that the lower state is more probable for the atom to be in as given by the Boltzmann Factor? so since it is more probable, it has more microstates and entropy causes it to decay? Please help...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in both s1 and s2. => union(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in either s1 or s2. =>difference(String s1, String s2): takes two strings and returns the string consisting of all letters that appear only in s1.
Python(please take a screen shot!): 1. hamming distance: write a function distance that take two bits...
Python(please take a screen shot!): 1. hamming distance: write a function distance that take two bits strings, you can assume each strings only contains 0's and 1's. (Note: the two strings might have the same length or not!) for example: hamming('010001001111', '01010100') should return 5(1 bit changed plus 4 bits "lost" from the end). 2. write a main function that ask user for two file names, open files and read the 1st line of each, and compares them using Hamming...
I'm trying to code in MIPS (MIPS Assembly Language) to calculate the hamming distance between two...
I'm trying to code in MIPS (MIPS Assembly Language) to calculate the hamming distance between two integers. Ideally, the program would ask for the user to type in the two integers. Then, the program would calculate the hamming distance. Afterward, it would ask if you'd like to find another hamming distance. If the user says yes, it would loop back to the beginning and ask for two new integers. Below is the code that I've created so far. Guidance with...
Problem 3 The function distance returns the distance between two strings X and Y. Its running...
Problem 3 The function distance returns the distance between two strings X and Y. Its running time is exponential. Explain why. (Note: make sure you understand what X[0: -1] is.) # # Input: X, Y, which are strings # Output: a number, which is the distance between the two strings # def distance(X, Y): if len(X)==0: return len(Y) if len(Y)==0: return len(X) if X[-1]==Y[-1]: return distance(X[0:-1], Y[0:-1]) else: a = distance(X[0:-1], Y[0:-1]) b = distance(X, Y[0:-1]) c = distance(X[0:-1], Y)...
if s1 and s2 are two simple functions then prove that the max and minimum of...
if s1 and s2 are two simple functions then prove that the max and minimum of then are also simple function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT