Longest common subsequence with permutations allowed | Javascript program to find LCS with permutations allowed ; Function to calculate longest String str1 -- > first String str2 -- > second String count1 [ ] -- > hash array to calculate frequency of characters in str1 count [ 2 ] -- > hash array to calculate frequency of characters in str2 result -- > resultant longest String whose permutations are sub - sequence of given two strings ; calculate frequency of characters ; Now traverse hash array ; append character ( ' a ' + i ) in resultant String ' result ' by min ( count1 [ i ] , count2i ] ) times ; Driver Code
function min ( a , b ) { if ( a < b ) return a ; else return b ; } function longestString ( str1 , str2 ) { var count1 = new Array ( 26 ) ; var count2 = new Array ( 26 ) ; count1 . fill ( 0 ) ; count2 . fill ( 0 ) ; for ( var i = 0 ; i < str1 . length ; i ++ ) { count1 [ str1 . charCodeAt ( i ) - 97 ] ++ ; } for ( var i = 0 ; i < str2 . length ; i ++ ) { count2 [ str2 . charCodeAt ( i ) - 97 ] ++ ; } var result = " " ; for ( var i = 0 ; i < 26 ; i ++ ) { for ( var j = 1 ; j <= min ( count1 [ i ] , count2 [ i ] ) ; j ++ ) { result += String . fromCharCode ( 97 + i ) ; } } document . write ( result ) ; } var str1 = " " ; var str2 = " " ; longestString ( str1 , str2 ) ;