I\'m very close on this. I got a question posed to me yesterday by a developer if I could have a look at this.
I feel close, but I think some people here would apprec
This works better than my first approach(striked out).
You can use following extension to get all substrings of the shortest string in the list(for efficiency):
public static IEnumerable getAllSubstrings(this string word)
{
return from charIndex1 in Enumerable.Range(0, word.Length)
from charIndex2 in Enumerable.Range(0, word.Length - charIndex1 + 1)
where charIndex2 > 0
select word.Substring(charIndex1, charIndex2);
}
Length(longest first)string shortest = list.OrderBy(s => s.Length).First();
IEnumerable shortestSubstrings = shortest
.getAllSubstrings()
.OrderByDescending(s => s.Length);
var other = list.Where(s => s != shortest).ToArray();
string longestCommonIntersection = string.Empty;
foreach (string subStr in shortestSubstrings)
{
bool allContains = other.All(s => s.Contains(subStr));
if (allContains)
{
longestCommonIntersection = subStr;
break;
}
}
DEMO