views:

379

answers:

3

i am looking for a longest common words c# implementation. Most of the samples i have came across are comparing character by character.

in otherwords,

string1 = access
string2 = advised 

should return null output from the function

any sample codes?

+2  A: 

I think this problem is usually referred to as the Longest common substring problem. The Wikipedia article contains pseudocode, and C# implementations can be found on the Web.

ghostskunks
but its problem is, it is comparing character by character, in my previous example i have mentioned two different words string1 = access string2 = advised in this case it returns "a" which is not what i want. it should return nothing because it is two different words.
kakopappa
A: 

If by word you mean these letter things, seperated from the others by punktuation, try this:

private String longestCommonWord(String s1, String s2)
    {
        String[] seperators = new String[] { " ", ",", ".", "!", "?", ";" };
        var result = from w1 in s1.Split(seperators, StringSplitOptions.RemoveEmptyEntries)
                     where (from w2 in s2.Split(seperators, StringSplitOptions.RemoveEmptyEntries)
                            where w2 == w1
                            select w2).Count() > 0
                     orderby w1.Length descending
                     select w1;
        if (result.Count() > 0)
        {
            return result.First();
        }
        else
        {
            return null;
        }
    }

This probably is not the most elegant way to do it, but it works for me. =)

Jens
Hi Jens, thanks for u r message. this linq query returns a one word.what i was expecting something like thisstring1 = "You are advised to grant access to these settings "string2 = "these settings "longestCommonWord(string1 , string2 )should return "these settings "
kakopappa
there is algorithm called "Longest common substring problem. " which solves this, but its problem is, it is comparing character by character, in my previous example i have mentioned two different words string1 = accessstring2 = advised this function should return nothing because it's two different words, but "Longest common substring problem" algorithm gives "a" which is not what i excpet
kakopappa
In this case, you have a "Longest common substring problem". If you call your words characters, maybe the algorythm can be modified to work an words instead? E.g. with A="You" B="are" C="adviced" D="access" E="these" F="Settings" your string1 equals ABCDEF, string2 equals EF, and you have reduced your problem to the one already solved.
Jens
+2  A: 

Turning the algorithm which computes LCS of arrays of characters into one that does it to arrays of anything else -- like, say, an array of words -- is usually pretty straightforward. Have you tried that?

If you need some hints, here's an article I wrote a couple years ago on how to implement Longest Common Subsequence on an array of words in JScript. You should be able to adapt it to C# without too much difficulty.

http://blogs.msdn.com/ericlippert/archive/2004/07/21/189974.aspx

Eric Lippert