forked from trekhleb/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
21 changes: 21 additions & 0 deletions
21
src/algorithms/string/hamming-distance/__test__/hammingDistance.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |