Calculate the Hamming distance between two strings - MATLAB Cody - MATLAB Central

Problem 846. Calculate the Hamming distance between two strings

Difficulty:Rate

Inspired by a similar Cody problem found here.

Parts of the problem description were taken from http://en.wikipedia.org/wiki/Hamming_distance.

The Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different. In other words, it is the number of substitutions required to transform one string into another. Given two strings of equal length, compute the Hamming distance.

Example

If

s1 = 'cats'
s2 = 'dogs'

then

d = 3

because three substitutions are required to transform s1 into s2.

   cats => dats (substitute 'd' for 'c')
   dats => dots (substitute 'o' for 'a')
   dots => dogs (substitute 'g' for 't')

Solution Stats

51.46% Correct | 48.54% Incorrect
Last Solution submitted on Apr 03, 2025

Problem Comments

Solution Comments

Show comments
Registration Now Open for MathWorks AUTOMOTIVE CONFERENCE 2025
...
Hello Community, We're excited to announce that registration is now open for the...

Group

Strings I Image
Strings I
  • 30 Problems
  • 102 Finishers

Problem Recent Solvers325

Problem Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Go to top of page