63. Longest common substring

The following code uses the obvious approach described in the problem statement that examines all of the first string's possible substrings:

// Find the longest substring by examining every possible// substring in string1.private string FindLongestSubstring1(string string1, string string2){    string bestSubstring = "";    int bestLength = 0;    // Loop over all possible starting positions.    for (int startPos = 0; startPos < string1.Length - 1; startPos++)    {        // Loop over possible lengths starting at this position.        for (int length = bestLength + 1;            length <= string1.Length - startPos;            length++)        {            string testSubstring = string1.Substring(startPos, length);            int testPos = string2.IndexOf(testSubstring);            if (testPos >= 0) { ...

Get The Modern C# Challenge now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.