longest common substring problem statement given two sequences find the longest common substring present in both of them a substring is necessarily continuous example abcdef and xabded have two longest common substrings ab or de therefore algorithm should return any one of them finds the longest common substring between two strings longestcommonsubstring longestcommonsubstringa longestcommonsubstring a longestcommonsubstringa a a longestcommonsubstringabcdef bcd bcd longestcommonsubstringabcdef xabded ab longestcommonsubstringgeeksforgeeks geeksquiz geeks longestcommonsubstringabcdxyz xyzabcd abcd longestcommonsubstringzxabcdezy yzabcdezx abcdez longestcommonsubstringoldsite geeksforgeeks org newsite geeksquiz com site geeks longestcommonsubstring1 1 traceback most recent call last valueerror longestcommonsubstring takes two strings for inputs finds the longest common substring between two strings longest_common_substring longest_common_substring a longest_common_substring a longest_common_substring a a a longest_common_substring abcdef bcd bcd longest_common_substring abcdef xabded ab longest_common_substring geeksforgeeks geeksquiz geeks longest_common_substring abcdxyz xyzabcd abcd longest_common_substring zxabcdezy yzabcdezx abcdez longest_common_substring oldsite geeksforgeeks org newsite geeksquiz com site geeks longest_common_substring 1 1 traceback most recent call last valueerror longest_common_substring takes two strings for inputs
def longest_common_substring(text1: str, text2: str) -> str:
    

    if not (isinstance(text1, str) and isinstance(text2, str)):
        raise ValueError("longest_common_substring() takes two strings for inputs")

    text1_length = len(text1)
    text2_length = len(text2)

    dp = [[0] * (text2_length + 1) for _ in range(text1_length + 1)]
    ans_index = 0
    ans_length = 0

    for i in range(1, text1_length + 1):
        for j in range(1, text2_length + 1):
            if text1[i - 1] == text2[j - 1]:
                dp[i][j] = 1 + dp[i - 1][j - 1]
                if dp[i][j] > ans_length:
                    ans_index = i
                    ans_length = dp[i][j]

    return text1[ans_index - ans_length : ans_index]


if __name__ == "__main__":
    import doctest

    doctest.testmod()