Cross two lists in the first order

I'm having a problem, I don’t even know what to look for in Google / Stack Overflow. So comment if you feel the need for further explanation, questions.

Basically, I want to cross two lists and return the resemblance to the saved order of the original value of the first row.

Example:

I have two lines that I convert to CharArray. I want to cross these two arrays and return values ​​that are similar , including / with the order of the first line (s1) .


As you can see, the first line contains E15 (in that particular order) as well as seconds.

So these two lines will return: {'E', '1', '5'}

string s1 = "E15QD(A)";
string s2 = "NHE15H";

The problem I am facing is that if I replaced "s2" with:

string s2 = "NQE18H" // Will return {'Q', 'E', '1' }

My operation will return: {'Q', 'E', '1'}

The result should be: {'E', '1'}, because Q does not follow the letter 1

Currently, my work is not the biggest effort, because I do not know what methods to use in .NET to do this.

Current code:

List<char> cA1 = s1.ToList();
List<char> cA2 = s2.ToList();

var result = cA1.Where(x => cA2.Contains(x)).ToList();

Feel free to help me, pointers in the right direction are acceptable, as well as a complete solution.

+4
source share
3 answers

This is the "longest common substring . "

, :

public static class StringExtensions
{
    public static IEnumerable<string> GetSubstrings(this string str)
    {
        if (string.IsNullOrEmpty(str))
            throw new ArgumentException("str must not be null or empty", "str");

        for (int c = 0; c < str.Length - 1; c++)
        {
            for (int cc = 1; c + cc <= str.Length; cc++)
            {
                yield return str.Substring(c, cc);
            }
        }
    }
}

LINQ:

string longestIntersection = "E15QD(A)".GetSubstrings()
    .Intersect("NQE18H".GetSubstrings())
    .OrderByDescending(s => s.Length)
    .FirstOrDefault();  // E1

Enumerable.Intersect , . : , , ( ) :

longString.GetSubstrings().Intersect(shortString.GetSubstrings())
+2

, :

string similar = null;

for (int i = 0; i < s1.Length; i++)
{
    string s = s1.Substring(0, i + 1);

     if (s2.Contains(s))
     {
         similar = s;
     }
}

char[] result = similar.ToCharArray();
+1

@TimSchmelter provided a link to this answer in the comments on the original post.

public int LongestCommonSubstring(string str1, string str2, out string sequence)
    {
        sequence = string.Empty;
        if (String.IsNullOrEmpty(str1) || String.IsNullOrEmpty(str2))
            return 0;

        int[,] num = new int[str1.Length, str2.Length];
        int maxlen = 0;
        int lastSubsBegin = 0;
        StringBuilder sequenceBuilder = new StringBuilder();

        for (int i = 0; i < str1.Length; i++)
        {
            for (int j = 0; j < str2.Length; j++)
            {
                if (str1[i] != str2[j])
                    num[i, j] = 0;
                else
                {
                    if ((i == 0) || (j == 0))
                        num[i, j] = 1;
                    else
                        num[i, j] = 1 + num[i - 1, j - 1];

                    if (num[i, j] > maxlen)
                    {
                        maxlen = num[i, j];
                        int thisSubsBegin = i - num[i, j] + 1;
                        if (lastSubsBegin == thisSubsBegin)
                        {//if the current LCS is the same as the last time this block ran
                            sequenceBuilder.Append(str1[i]);
                        }
                        else //this block resets the string builder if a different LCS is found
                        {
                            lastSubsBegin = thisSubsBegin;
                            sequenceBuilder.Length = 0; //clear it
                            sequenceBuilder.Append(str1.Substring(lastSubsBegin, (i + 1) - lastSubsBegin));
                        }
                    }
                }
            }
        }
        sequence = sequenceBuilder.ToString();
        return maxlen;
    }
0
source

All Articles