Print the longest common substring | function to find and print the longest common substring of X [ 0. . m - 1 ] and Y [ 0. . n - 1 ] ; Create a table to store lengths of longest common suffixes of substrings . Note that LCSuff [ i ] [ j ] contains length of longest common suffix of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] . The first row and first column entries have no logical meaning , they are used only for simplicity of program ; To store length of the longest common substring ; To store the index of the cell which contains the maximum value . This cell 's index helps in building  up the longest common substring  from right to left. ; Following steps build LCSuff [ m + 1 ] [ n + 1 ] in bottom up fashion . ; if true , then no common substring exists ; allocate space for the longest common substring ; traverse up diagonally form the ( row , col ) cell until LCSuff [ row ] [ col ] != 0 ; move diagonally up to previous cell ; required longest common substring ; Driver Code
def printLCSSubStr ( X : str , Y : str , m : int , n : int ) : NEW_LINE INDENT LCSuff = [ [ 0 for i in range ( n + 1 ) ] for j in range ( m + 1 ) ] NEW_LINE length = 0 NEW_LINE row , col = 0 , 0 NEW_LINE for i in range ( m + 1 ) : NEW_LINE INDENT for j in range ( n + 1 ) : NEW_LINE INDENT if i == 0 or j == 0 : NEW_LINE INDENT LCSuff [ i ] [ j ] = 0 NEW_LINE DEDENT elif X [ i - 1 ] == Y [ j - 1 ] : NEW_LINE INDENT LCSuff [ i ] [ j ] = LCSuff [ i - 1 ] [ j - 1 ] + 1 NEW_LINE if length < LCSuff [ i ] [ j ] : NEW_LINE INDENT length = LCSuff [ i ] [ j ] NEW_LINE row = i NEW_LINE col = j NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT LCSuff [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT DEDENT if length == 0 : NEW_LINE INDENT print ( " No ▁ Common ▁ Substring " ) NEW_LINE return NEW_LINE DEDENT resultStr = [ '0' ] * length NEW_LINE while LCSuff [ row ] [ col ] != 0 : NEW_LINE INDENT length -= 1 NEW_LINE row -= 1 NEW_LINE col -= 1 NEW_LINE DEDENT print ( ' ' . join ( resultStr ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = " OldSite : GeeksforGeeks . org " NEW_LINE Y = " NewSite : GeeksQuiz . com " NEW_LINE m = len ( X ) NEW_LINE n = len ( Y ) NEW_LINE printLCSSubStr ( X , Y , m , n ) NEW_LINE DEDENT