Created
March 3, 2023 12:16
-
-
Save martingaido/af58fbe20350a6e4d43236535d195e80 to your computer and use it in GitHub Desktop.
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
/* | |
Hamming distance is a measure of the difference between | |
two same-length strings of characters. Simply put, it is | |
defined as the number of positions at which corresponding | |
characters in two strings of characters are different. | |
Formally, the Hamming distance between two same-length | |
strings of characters, s and t, is defined as the number | |
of positions at which the corresponding characters are | |
different: | |
d(s, t) = ∑i |si ≠ ti| | |
where si and ti are the characters at the i-th position of the | |
strings s and t, respectively, and |si ≠ ti| is the indicator | |
function that takes the value 1 if si ≠ ti, and 0 otherwise. | |
The Hamming distance is commonly used in information theory | |
and error-correcting codes to measure the amount of errors | |
that occur when transmitting data between two points. | |
It also has applications in molecular biology for comparing | |
sequences of DNA and proteins. | |
Source: ChatGPT | |
*/ | |
function hammingDistance(_string1, _string2) { | |
if (_string1.length !== _string2.length) { | |
return -1; | |
} | |
let distance = 0; | |
const L = _string1.length; | |
for (let i = 0; i < L; i++) { | |
if (_string1[i] !== _string2[i]) { | |
distance++; | |
} | |
} | |
return distance; | |
} | |
const result = hammingDistance("We're testing the program", "We're testing the machine"); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment