This post is completed by 1 user
|
Add to List |
415. Hamming Distance between two given strings
Objective: Given two strings with equal lengths, write an algorithm to calculate the hamming distance between the strings.
Hamming Distance: Hamming distance between two strings is the number of positions at which the characters are different.
Example:
X = AABBCCDD, Y = AAAACCCC Hamming distance: 4 There are four positions at which bits are different X = dogandcat, Y = catanddog Hamming distance: 6 There are six positions at which bits are different.
Approach:
- Initialize humming_distance = 0.
- Iterate both strings, one character at a time. If characters are different then increment humming_distance by 1.
- Once the iteration is completed, return the humming_distance.
Output:
x=AABBCCDD, y=AAAACCCC Hamming distance: 4 x=dogandcat, y=catanddog Hamming distance: 6