Longest Common Substring 28-12-2015 15:58:47 C# / Alogrithms 0 Bookmark(s) 196 View(s) public static string GetLongestCommonSubstring(params string[] strings) { var commonSubstrings = new HashSet(strings[0].GetSubstrings()); foreach (string str in strings.Skip(1)) { commonSubstrings.IntersectWith(str.GetSubstrings()); if (commonSubstrings.Count == 0) return string.Empty; } return commonSubstrings.OrderByDescending(s => s.Length).DefaultIfEmpty(string.Empty).First(); } private static IEnumerable GetSubstrings(this string 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); } } }