Cody

# Problem 846. Calculate the Hamming distance between two strings

Solution 1028887

Submitted on 24 Oct 2016 by Dan Po
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
s1 = 'Sunday'; s2 = 'Monday'; d_correct = 2; assert(isequal(hamming(s1,s2),d_correct))

a = 1×6 int16 row vector 83 117 110 100 97 121 b = 1×6 int16 row vector 77 111 110 100 97 121 g = 1×6 logical array 1 1 0 0 0 0 d = 2

2   Pass
s1 = 'Cody is not fun?'; s2 = 'Cody is awesome!'; d_correct = 8; assert(isequal(hamming(s1,s2),d_correct))

a = 1×16 int16 row vector 67 111 100 121 32 105 115 32 110 111 116 32 102 117 110 63 b = 1×16 int16 row vector 67 111 100 121 32 105 115 32 97 119 101 115 111 109 101 33 g = 1×16 logical array 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 d = 8

3   Pass
s1 = 'I do not like MATLAB'; s2 = 'I really love MATLAB'; d_correct = 8; assert(isequal(hamming(s1,s2),d_correct))

a = 1×20 int16 row vector Columns 1 through 16 73 32 100 111 32 110 111 116 32 108 105 107 101 32 77 65 Columns 17 through 20 84 76 65 66 b = 1×20 int16 row vector Columns 1 through 16 73 32 114 101 97 108 108 121 32 108 111 118 101 32 77 65 Columns 17 through 20 84 76 65 66 g = 1×20 logical array 0 0 1 1 1 1 1 1 0 0 1 1 0 0 0 0 0 0 0 0 d = 8

4   Pass
s1 = 'abcdefghijklmnopqrstuvwxyz'; s2 = 'AbcdEfghIjklmnOpqrstUvwxyz'; d_correct = 5; assert(isequal(hamming(s1,s2),d_correct))

a = 1×26 int16 row vector Columns 1 through 16 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 Columns 17 through 26 113 114 115 116 117 118 119 120 121 122 b = 1×26 int16 row vector Columns 1 through 16 65 98 99 100 69 102 103 104 73 106 107 108 109 110 79 112 Columns 17 through 26 113 114 115 116 85 118 119 120 121 122 g = 1×26 logical array Columns 1 through 24 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 Columns 25 through 26 0 0 d = 5

5   Pass
s1 = '1234567891011121314151617181920'; s2 = '2019181716151413121110987654321'; d_correct = 27; assert(isequal(hamming(s1,s2),d_correct))

a = 1×31 int16 row vector Columns 1 through 19 49 50 51 52 53 54 55 56 57 49 48 49 49 49 50 49 51 49 52 Columns 20 through 31 49 53 49 54 49 55 49 56 49 57 50 48 b = 1×31 int16 row vector Columns 1 through 19 50 48 49 57 49 56 49 55 49 54 49 53 49 52 49 51 49 50 49 Columns 20 through 31 49 49 48 57 56 55 54 53 52 51 50 49 g = 1×31 logical array Columns 1 through 24 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 Columns 25 through 31 0 1 1 1 1 0 1 d = 27

6   Pass
s1 = 'race car'; s2 = 'rac ecar'; d_correct = 2; assert(isequal(hamming(s1,s2),d_correct))

a = 1×8 int16 row vector 114 97 99 101 32 99 97 114 b = 1×8 int16 row vector 114 97 99 32 101 99 97 114 g = 1×8 logical array 0 0 0 1 1 0 0 0 d = 2

7   Pass
s1 = 'Are these strings identical?'; s2 = 'Are these strings identical?'; d_correct = 0; assert(isequal(hamming(s1,s2),d_correct))

a = 1×28 int16 row vector Columns 1 through 16 65 114 101 32 116 104 101 115 101 32 115 116 114 105 110 103 Columns 17 through 28 115 32 105 100 101 110 116 105 99 97 108 63 b = 1×28 int16 row vector Columns 1 through 16 65 114 101 32 116 104 101 115 101 32 115 116 114 105 110 103 Columns 17 through 28 115 32 105 100 101 110 116 105 99 97 108 63 g = 1×28 logical array Columns 1 through 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 25 through 28 0 0 0 0 d = 0

8   Pass
s1 = ''; s2 = ''; d_correct = 0; assert(isequal(hamming(s1,s2),d_correct))

a = 0×0 empty int16 matrix b = 0×0 empty int16 matrix g = 0×0 empty logical array d = 0