- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathHammingDistance.cs
37 lines (33 loc) · 1.1 KB
/
HammingDistance.cs
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
27
28
29
30
31
32
33
34
35
36
37
usingSystem;
namespaceAlgorithms.Strings.Similarity;
/// <summary>
/// <para>
/// Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different.
/// Time complexity is O(n) where n is the length of the string.
/// </para>
/// <para>
/// Wikipedia: https://en.wikipedia.org/wiki/Hamming_distance.
/// </para>
/// </summary>
publicstaticclassHammingDistance
{
/// <summary>
/// Calculates Hamming distance between two strings of equal length.
/// </summary>
/// <param name="s1">First string.</param>
/// <param name="s2">Second string.</param>
/// <returns>Levenshtein distance between source and target strings.</returns>
publicstaticintCalculate(strings1,strings2)
{
if(s1.Length!=s2.Length)
{
thrownewArgumentException("Strings must be equal length.");
}
vardistance=0;
for(vari=0;i<s1.Length;i++)
{
distance+=s1[i]!=s2[i]?1:0;
}
returndistance;
}
}