两个字符串之间的所有共同子串 [英] All Common Substrings Between Two Strings

查看:140
本文介绍了两个字符串之间的所有共同子串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的C#找两个字符串之间的所有常见的字符串。例如,如果输入是:
S1 =需要用电子邮件asssitance
S2 =电子邮件援助需要



的输出应 - 需要帮助电子邮件



下面的代码返回最长公共子,但我想我的代码返回所有常见的字符串。任何帮助深表感谢!

 静态无效commonsubstrings()
{
输入1 =需要电子邮件asssitance
输入2 =电子邮件需要援助

如果(input2.Length> input1.Length)
{
互换=输入1;
输入1 =输入2;
输入2 =掉;
}

INT K = 1;
串温度;
字符串longTemp =;
的for(int i = 0;(I< = input1.Length);我++)
{
如果((我== input1.Length))
{
如果(最长!= NULL)
{
K = longest.Length + 1;
}
,否则
{
K = 1;
}
TEMP = input1.Substring(1 input1.Length - 1);
如果(temp.Equals())
{
中断;
}
如果(K< = temp.Length)
{
I = K - 1;
输入1 =温度;
如果(!(最长= NULL)及及(longest.Length> longTemp.Length))
{
longTemp =最长的;
}
}
}
holder1 = input1.Substring(0,K);

为(INT J = 0;(J< input2.Length)及及(J + K< = input2.Length); J ++)
{
CHECK1 = input2.Substring(J,K);
如果(holder1.Equals(CHECK1))
{
最长= holder1;
中断;
}
}
k的序列++;
}

Console.WriteLine(最长);
到Console.ReadLine();



}


解决方案

 公共静态字符串[] CommonString(左字符串,字符串右)
{
名单,LT;字符串>结果=新的List<串GT;();
的String [] = rightArray right.Split();
的String [] = leftArray left.Split();

result.AddRange(rightArray.Where(R => leftArray.Any(L => l.StartsWith(R))));

//时,必须注意检查其他方式离开阵列包含比右边的数组
result.AddRange较小的字(leftArray.Where(L => rightArray.Any(R = GT; R。 StartsWith(1))));

返回result.Distinct()ToArray的()。
}


I am working on C# to find all the common substrings between two strings. For instance, if the input is: S1= "need asssitance with email" S2= "email assistance needed"

The output should be- 'need assistance email'

The below code returns the longest common substring, but I want my code to return all the common substrings. Any help is much appreciated!

static void commonsubstrings()
    {
        input1 = "need asssitance with email";
        input2 = "email assistance needed"

        if (input2.Length > input1.Length)
        {
            swap = input1;
            input1 = input2;
            input2 = swap;
        }

        int k = 1;
        String temp;
        String longTemp = "";
        for (int i = 0; (i <= input1.Length); i++)
        {
            if ((i == input1.Length))
            {
                if (longest != null)
                {
                    k = longest.Length + 1;
                }
                else
                {
                    k = 1;
                }
                temp = input1.Substring(1, input1.Length - 1);
                if (temp.Equals(""))
                {
                    break;
                }
                if (k <= temp.Length)
                {
                    i = k - 1;
                    input1 = temp;
                    if ((longest != null) && (longest.Length > longTemp.Length))
                    {
                        longTemp = longest;
                    }
                }
            }
            holder1 = input1.Substring(0, k);

            for (int j = 0; (j < input2.Length) && (j + k <= input2.Length); j++)
            {
                check1 = input2.Substring(j, k);
                if (holder1.Equals(check1))
                {
                    longest = holder1;
                    break;
                }
            }
            k++;
        }

        Console.WriteLine(longest);
        Console.ReadLine(); 

}

解决方案

    public static string [] CommonString(string left, string right)
    {
        List<string> result = new List<string>();
        string [] rightArray = right.Split();
        string [] leftArray = left.Split();

        result.AddRange(rightArray.Where(r => leftArray.Any(l => l.StartsWith(r))));

        // must check other way in case left array contains smaller words than right array
        result.AddRange(leftArray.Where(l => rightArray.Any(r => r.StartsWith(l))));

        return result.Distinct().ToArray();
    }

这篇关于两个字符串之间的所有共同子串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆