Skip to content

algorithm: Added Hamming Distance #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* [Fibonacci](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/fibonacci.ts)
* [Find Min](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/find_min.ts)
* [Greatest Common Factor](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/greatest_common_factor.ts)
* [Hamming Distance](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/hamming_distance.ts)
* [Is Divisible](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_divisible.ts)
* [Is Even](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_even.ts)
* [Is Leap Year](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_leap_year.ts)
Expand Down
25 changes: 25 additions & 0 deletions maths/hamming_distance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @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 }
5 changes: 5 additions & 0 deletions maths/test/hamming_distance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { HammingDistance } from "../hamming_distance"

test.each([['happy', 'homie', 4], ['hole', 'home', 1], ['cathrine', 'caroline', 3], ['happiness', 'dizziness', 4]])('Hamming Distance', (str1, str2, result) => {
expect(HammingDistance(str1, str2)).toBe(result)
})