Find the longest common prefix between two strings after performing swaps on second string | JavaScript program to find the long common prefix between two strings after performing swaps on the second string ; let a = x . length ; length of x let b = y . length ; length of y ; creating frequency array of characters of y ; storing the length of longest common prefix ; checking if the frequency of the character at position i in x in b is greater than zero or not if zero we increase the prefix count by 1 ; driver code
function LengthLCP ( x , y ) { let fr = Array ( 26 ) . fill ( 0 ) ; for ( let i = 0 ; i < b ; i ++ ) { fr [ y [ i ] . charCodeAt ( ) - 97 ] += 1 ; } let c = 0 ; for ( let i = 0 ; i < a ; i ++ ) { if ( fr [ x [ i ] . charCodeAt ( ) - 97 ] > 0 ) { c += 1 ; fr [ x [ i ] . charCodeAt ( ) - 97 ] -= 1 ; } else break ; } document . write ( ( c ) ) ; } let x = " " , y = " " ; LengthLCP ( x , y ) ;