Find two equal subsequences of maximum length with at least one different index | Python implementation of the approach ; Function to return the required length of the subsequences ; To store the result ; To store the last visited position of lowercase letters ; Initialisation of frequency array to - 1 to indicate no character has previously occured ; For every character of the String ; Get the index of the current character ; If the current character has appeared before in the String ; Update the result ; Update the last position of the current character ; Driver code
MAX = 26 ; NEW_LINE def maxLength ( str , len ) : NEW_LINE INDENT res = 0 ; NEW_LINE lastPos = [ 0 ] * MAX ; NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT lastPos [ i ] = - 1 ; NEW_LINE DEDENT for i in range ( len ) : NEW_LINE INDENT C = ord ( str [ i ] ) - ord ( ' a ' ) ; NEW_LINE if ( lastPos [ C ] != - 1 ) : NEW_LINE INDENT res = max ( len - ( i - lastPos [ C ] - 1 ) - 1 , res ) ; NEW_LINE DEDENT lastPos [ C ] = i ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " geeksforgeeks " ; NEW_LINE len = len ( str ) ; NEW_LINE print ( maxLength ( str , len ) ) ; NEW_LINE DEDENT