Skip to content

Commit 66ebd78

Browse files
committed
Add Hamming.
1 parent 6cf17e4 commit 66ebd78

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* [Fisher–Yates Shuffle](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fisher-yates) - random permutation of a finite sequence
4141
* **String**
4242
* [Levenshtein Distance](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences
43-
* Hamming
43+
* [Hamming Distance](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/hamming-distance) - number of positions at which the symbols are different
4444
* Huffman
4545
* Knuth Morris Pratt
4646
* Longest common subsequence
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Hamming Distance
2+
3+
the Hamming distance between two strings of equal length is the
4+
number of positions at which the corresponding symbols are
5+
different. In other words, it measures the minimum number of
6+
substitutions required to change one string into the other, or
7+
the minimum number of errors that could have transformed one
8+
string into the other. In a more general context, the Hamming
9+
distance is one of several string metrics for measuring the
10+
edit distance between two sequences.
11+
12+
## Examples
13+
14+
The Hamming distance between:
15+
16+
- "ka**rol**in" and "ka**thr**in" is **3**.
17+
- "k**a**r**ol**in" and "k**e**r**st**in" is **3**.
18+
- 10**1**1**1**01 and 10**0**1**0**01 is **2**.
19+
- 2**17**3**8**96 and 2**23**3**7**96 is **3**.
20+
21+
## References
22+
23+
[Wikipedia](https://en.wikipedia.org/wiki/Hamming_distance)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import hammingDistance from '../hammingDistance';
2+
3+
describe('hammingDistance', () => {
4+
it('should throw an error when trying to compare the strings of different lengths', () => {
5+
const compareStringsOfDifferentLength = () => {
6+
hammingDistance('a', 'aa');
7+
};
8+
9+
expect(compareStringsOfDifferentLength).toThrowError();
10+
});
11+
12+
it('should calculate difference between two strings', () => {
13+
expect(hammingDistance('a', 'a')).toBe(0);
14+
expect(hammingDistance('a', 'b')).toBe(1);
15+
expect(hammingDistance('abc', 'add')).toBe(2);
16+
expect(hammingDistance('karolin', 'kathrin')).toBe(3);
17+
expect(hammingDistance('karolin', 'kerstin')).toBe(3);
18+
expect(hammingDistance('1011101', '1001001')).toBe(2);
19+
expect(hammingDistance('2173896', '2233796')).toBe(3);
20+
});
21+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {string} a
3+
* @param {string} b
4+
* @return {number}
5+
*/
6+
export default function hammingDistance(a, b) {
7+
if (a.length !== b.length) {
8+
throw new Error('Strings must be of the same length');
9+
}
10+
11+
let distance = 0;
12+
13+
for (let i = 0; i < a.length; i += 1) {
14+
if (a[i] !== b[i]) {
15+
distance += 1;
16+
}
17+
}
18+
19+
return distance;
20+
}

0 commit comments

Comments
 (0)