Longest string which is prefix string of at least two strings | Python3 program to find longest string which is prefix string of at least two strings ; Function to find max length of the prefix ; Base case ; Iterating over all the alphabets ; Checking if char exists in current string or not ; If atleast 2 string have that character ; Recursive call to i + 1 ; Driver code ; Initialising strings ; Push strings into vectors .
max1 = 0 NEW_LINE def MaxLength ( v , i , m ) : NEW_LINE INDENT global max1 NEW_LINE if ( i >= m ) : NEW_LINE INDENT return m - 1 NEW_LINE DEDENT for k in range ( 26 ) : NEW_LINE INDENT c = chr ( ord ( ' a ' ) + k ) NEW_LINE v1 = [ ] NEW_LINE for j in range ( len ( v ) ) : NEW_LINE INDENT if ( v [ j ] [ i ] == c ) : NEW_LINE INDENT v1 . append ( v [ j ] ) NEW_LINE DEDENT DEDENT if ( len ( v1 ) >= 2 ) : NEW_LINE INDENT max1 = max ( max1 , MaxLength ( v1 , i + 1 , m ) ) NEW_LINE DEDENT else : NEW_LINE INDENT max1 = max ( max1 , i - 1 ) NEW_LINE DEDENT DEDENT return max1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s1 = " abcde " NEW_LINE s2 = " abcsd " NEW_LINE s3 = " bcsdf " NEW_LINE s4 = " abcda " NEW_LINE s5 = " abced " NEW_LINE v = [ ] NEW_LINE v . append ( s1 ) NEW_LINE v . append ( s2 ) NEW_LINE v . append ( s3 ) NEW_LINE v . append ( s4 ) NEW_LINE v . append ( s5 ) NEW_LINE m = len ( v [ 0 ] ) NEW_LINE print ( MaxLength ( v , 0 , m ) + 1 ) NEW_LINE DEDENT