Find two equal subsequences of maximum length with at least one different index | Javascript 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
var MAX = 26 ; function maxLength ( str , len ) { var res = 0 ; var lastPos = Array ( MAX ) ; for ( var i = 0 ; i < MAX ; i ++ ) { lastPos [ i ] = - 1 ; } for ( var i = 0 ; i < len ; i ++ ) { var C = str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( lastPos [ C ] != - 1 ) { res = Math . max ( len - ( i - lastPos [ C ] - 1 ) - 1 , res ) ; } lastPos [ C ] = i ; } return res ; } var str = " " ; var len = str . length ; document . write ( maxLength ( str , len ) ) ;