user
Task: Generate an algorithm to find the longest substring of two strings


assistant
def longest_common_substring(str1, str2): 
    n1 = len(str1) 
    n2 = len(str2) 
  
    LCSuff = [[0 for i in range(n2+1)] for j in range(n1+1)] 
  
    res = "" # To store the result 
    lenght = 0 # To store the lenght of the result 
  
    # building up theLCSuff[][] in bottom up manner 
    for i in range(n1+1): 
        for j in range(n2+1): 
            if (i == 0 or j == 0): 
                LCSuff[i][j] = 0
            elif (str1[i-1] == str2[j-1]): 
                LCSuff[i][j] = LCSuff[i-1][j-1] + 1
                if (lenght < LCSuff[i][j]): 
                    lenght = LCSuff[i][j] 
                    res = str1[i-lenght: i] 
            else: 
                LCSuff[i][j] = 0
  
    return res