Problem: Longest common substring
I JUST WRITE THE ALGORITHM FOR THIS PROBLEM AND IT CRASHES AT THE LAST TEST. I’VE EVEN CONSIDERED THE CASE, WHERE THERE IS NO SUCH SUBSTRING HERE IS MY CODE: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { var words = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); var l = Longest(words[0], words[1]); //Console.WriteLine(string.Join(" ", l)); for(int i = 2; i < words.Length; i++) { l = l.Where(x => words[i].Contains(x)).ToList(); } //Console.WriteLine(string.Join(" ", l)); Console.WriteLine(l[l.Count - 1]); } static List<string> Longest(string s1, string s2) { int m = Math.Min(s1.Length, s2.Length); List<string> l = new List<string>(); l.Add(""); int maxLen = 0; for(int i = 0; i < s1.Length; i++) { for(int j = 0; j < s2.Length; j++) { if(s1[i] == s2[j]) { int k = -1; string s = ""; while(i + ++k < s1.Length && j + k < s2.Length && s1[i + k] == s2[j + k]) { s += s1[i + k]; } l.Add(s); } } } l = l.OrderBy(x => x.Length).ThenByDescending(x => x).ToList(); return l; } } }