获取两个字符串之间的不同字符数,允许定义数量的差异

时间:2013-11-27 05:02:58

标签: c# char

我想要一个接收输入two char arrays的方法和一个整数d,它表示两个数组中字符之间的最大差异,并返回true或false,具体取决于数组是否相似,最多不同与d chars

所以例如

char[] a1 = { 's', 't', 'a', 'f', 'f' };
char[] a2 = { 's', 't', 'a', 'c', 'k' };

并且d = 2将返回true,因为那些数组几乎相似

我正在考虑制作一个for循环并比较每个char并有一个maxDiference计数器,如果计数器超过return false,就像:

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;



namespace auxros
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] a1 = { 's', 't', 'a', 'f', 'f' };
            char[] a2 = { 's', 't', 'a', 'c', 'k' };
            bool areDifferent = CharMismatches(a1, a2, 1);
            System.Console.WriteLine("The arrays are diferent at most by one char? " + areDifferent);
            areDifferent = CharMismatches(a1, a2, 2);
            System.Console.WriteLine("The arrays are diferent at most by two chars? " + areDifferent);
            areDifferent = CharMismatches(a1, a2, 3);
            System.Console.WriteLine("The arrays are diferent at most by three chars? " + areDifferent);
        }

        static bool CharMismatches(char[] a1, char[] a2, int d) {
            int mismatches = 0;
            for (int i = 0; i < a1.Length; i++)
            {
                if (!a1[i].Equals(a2[i]))
                {
                    mismatches++;
                }
                if (mismatches == d)
                {
                    return true;
                }
            }            
            if (mismatches <= d)
            {
                return false;
            }
            return true;

        }
    }
}

有更有效的方法吗?

2 个答案:

答案 0 :(得分:2)

为什么没有CharMismatches返回号码d(最多不超过)?

static int CharMismatches(char[] a1, char[] a2, int max) 
{
    int mismatches = 0;
    for (int i = 0; i < a1.Length; i++)
    {
        if (!a1[i].Equals(a2[i]))
            if (++mismatches == max)
                return mismatches
    }            
    return mismatches;
}

这样,你只需要运行一次:

int max = 3;
int mismatches = CharMismatches(a1, a2, max);
System.Console.WriteLine(string.Format(
    "The arrays are different at {1} by {0} char", 
    mismatches,
    mismatches == max ? "least" : "most"
));

答案 1 :(得分:1)

检查整个阵列中是否存在不匹配。

public class CharCount
{
    public char Char { get; set; }
    public int FirstArrayCount { get; set; }
    public int SecondArrayCount { get; set; }
}

private static int CharMismatches(char[] a1, char[] a2)
{
    var charCountList = new List<CharCount>();

    charCountList.AddRange(a1.GroupBy(a => a).Select(a => new CharCount { Char = a.Key, FirstArrayCount = a.Count() }).ToList());

    charCountList.AddRange(a2.GroupBy(a => a).Select(a => new CharCount { Char = a.Key, SecondArrayCount = a.Count() }).ToList());

    var totalCharCountList= charCountList.GroupBy(a => a.Char).ToDictionary(a => a.Key, b => new CharCount
        {
            Char = b.Key,
            FirstArrayCount = b.Sum(c => c.FirstArrayCount),
            SecondArrayCount = b.Sum(c => c.SecondArrayCount)
        });

    var totalMisMatches= totalCharCountList.Count(a => a.Value.FirstArrayCount == 0 || a.Value.SecondArrayCount==0);
    return totalMisMatches;
}