1 Star 0 Fork 65

欢喜派 / C-Sharp

forked from 编程语言算法集 / C-Sharp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
HammingDistance.cs 1.21 KB
一键复制 编辑 原始数据 按行查看 历史
Valdas 提交于 2022-10-06 10:14 . Add Hamming distance (#339)
using System;
namespace Algorithms.Strings
{
/// <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>
public static class HammingDistance
{
/// <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>
public static int Calculate(string s1, string s2)
{
if(s1.Length != s2.Length)
{
throw new ArgumentException("Strings must be equal length.");
}
var distance = 0;
for (var i = 0; i < s1.Length; i++)
{
distance += s1[i] != s2[i] ? 1 : 0;
}
return distance;
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/Joypai/C-Sharp.git
git@gitee.com:Joypai/C-Sharp.git
Joypai
C-Sharp
C-Sharp
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891