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

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)...
Given two unsorted arrays of integers. a) Write a pseudocode algorithm which will output only the...
Given two unsorted arrays of integers. a) Write a pseudocode algorithm which will output only the integers not common to both arrays. When writing pseudocode, consider that no implementation for data structures or algorithms exist. b) Implement your algorithm in Modern C++
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.
The intersection (∩) of two sets (s1, s2) is the set of all elements that are...
The intersection (∩) of two sets (s1, s2) is the set of all elements that are in s1 and are also in s2. Write a function (intersect) that takes two lists as input (you can assume they have no duplicate elements), and returns the intersection of those two sets (as a list) without using the in operator or any built-in functions, except for range() and len(). Write some code to test your function, as well. Note: Do not use the...
Suppose you have two strains of mice, S1 and S2. Strain S2 is genetically modified to...
Suppose you have two strains of mice, S1 and S2. Strain S2 is genetically modified to metabolize a pharmacon P supposedly faster than S1. You conducted an experiment in a sample set of each strain, in which the pharmacon was injected and its concentration in blood was measured every 15min for 2h. Of course, age, gender, and weight was recorded for each animal. You want to statistically demonstrate that the metabolic rate of N is higher in S2 than S1....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT