mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-25 22:46:20 +08:00
Add Hamming.
This commit is contained in:
parent
6cf17e4afb
commit
66ebd7859a
@ -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
|
||||
|
23
src/algorithms/string/hamming-distance/README.md
Normal file
23
src/algorithms/string/hamming-distance/README.md
Normal file
@ -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)
|
@ -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
src/algorithms/string/hamming-distance/hammingDistance.js
Normal file
20
src/algorithms/string/hamming-distance/hammingDistance.js
Normal file
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user