-
-
Notifications
You must be signed in to change notification settings - Fork 408
/
Copy pathhamming_distance.ts
26 lines (22 loc) · 1.05 KB
/
hamming_distance.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* @function hammingDistance
* @description Returns the Hamming distance between two strings of equal length
* @summary 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 to the other
* It is one of the several sring metrics, used fror measuring the edit distance between two sequences
* and is named after the American mathematician Richard Hamming
* @param str1 One of the strings to compare to the other
* @param str2 One of the strings to compare to the other
* @returns {number}
* @see [Wikipedia](https://en.wikipedia.org/wiki/Hamming_distance)
* @example hammingDistance('happy', 'homie')
*/
const hammingDistance = (str1: string, str2: string) => {
if (str1.length !== str2.length)
throw new Error('Strings must of the same length.')
let dist = 0
for (let i = 0; i < str1.length; i++) if (str1[i] !== str2[i]) dist++
return dist
}
export { hammingDistance }