From 66ebd7859a3daa2e649d6bd97076692fb20ab717 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Tue, 24 Apr 2018 14:45:55 +0300 Subject: [PATCH] Add Hamming. --- README.md | 2 +- .../string/hamming-distance/README.md | 23 +++++++++++++++++++ .../__test__/hammingDistance.test.js | 21 +++++++++++++++++ .../hamming-distance/hammingDistance.js | 20 ++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/algorithms/string/hamming-distance/README.md create mode 100644 src/algorithms/string/hamming-distance/__test__/hammingDistance.test.js create mode 100644 src/algorithms/string/hamming-distance/hammingDistance.js diff --git a/README.md b/README.md index c2ca1639..0e241337 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/algorithms/string/hamming-distance/README.md b/src/algorithms/string/hamming-distance/README.md new file mode 100644 index 00000000..516a8e03 --- /dev/null +++ b/src/algorithms/string/hamming-distance/README.md @@ -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) diff --git a/src/algorithms/string/hamming-distance/__test__/hammingDistance.test.js b/src/algorithms/string/hamming-distance/__test__/hammingDistance.test.js new file mode 100644 index 00000000..e8e8fd7a --- /dev/null +++ b/src/algorithms/string/hamming-distance/__test__/hammingDistance.test.js @@ -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); + }); +}); diff --git a/src/algorithms/string/hamming-distance/hammingDistance.js b/src/algorithms/string/hamming-distance/hammingDistance.js new file mode 100644 index 00000000..3749e78b --- /dev/null +++ b/src/algorithms/string/hamming-distance/hammingDistance.js @@ -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; +}