Skip to content

Commit

Permalink
Add Hamming.
Browse files Browse the repository at this point in the history
  • Loading branch information
trekhleb committed Apr 24, 2018
1 parent 6cf17e4 commit 66ebd78
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* [Fisher–Yates Shuffle](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fisher-yates) - random permutation of a finite sequence
* **String**
* [Levenshtein Distance](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences
* Hamming
* [Hamming Distance](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/hamming-distance) - number of positions at which the symbols are different
* Huffman
* Knuth Morris Pratt
* Longest common subsequence
Expand Down
23 changes: 23 additions & 0 deletions src/algorithms/string/hamming-distance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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 measures the minimum number of
substitutions required to change one string into the other, or
the minimum number of errors that could have transformed one
string into the other. In a more general context, the Hamming
distance is one of several string metrics for measuring the
edit distance between two sequences.

## Examples

The Hamming distance between:

- "ka**rol**in" and "ka**thr**in" is **3**.
- "k**a**r**ol**in" and "k**e**r**st**in" is **3**.
- 10**1**1**1**01 and 10**0**1**0**01 is **2**.
- 2**17**3**8**96 and 2**23**3**7**96 is **3**.

## References

[Wikipedia](https://en.wikipedia.org/wiki/Hamming_distance)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import hammingDistance from '../hammingDistance';

describe('hammingDistance', () => {
it('should throw an error when trying to compare the strings of different lengths', () => {
const compareStringsOfDifferentLength = () => {
hammingDistance('a', 'aa');
};

expect(compareStringsOfDifferentLength).toThrowError();
});

it('should calculate difference between two strings', () => {
expect(hammingDistance('a', 'a')).toBe(0);
expect(hammingDistance('a', 'b')).toBe(1);
expect(hammingDistance('abc', 'add')).toBe(2);
expect(hammingDistance('karolin', 'kathrin')).toBe(3);
expect(hammingDistance('karolin', 'kerstin')).toBe(3);
expect(hammingDistance('1011101', '1001001')).toBe(2);
expect(hammingDistance('2173896', '2233796')).toBe(3);
});
});
20 changes: 20 additions & 0 deletions src/algorithms/string/hamming-distance/hammingDistance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {string} a
* @param {string} b
* @return {number}
*/
export default function hammingDistance(a, b) {
if (a.length !== b.length) {
throw new Error('Strings must be of the same length');
}

let distance = 0;

for (let i = 0; i < a.length; i += 1) {
if (a[i] !== b[i]) {
distance += 1;
}
}

return distance;
}

0 comments on commit 66ebd78

Please sign in to comment.