Count common subsequence in two strings | return the number of common subsequence in two strings ; for each character of S ; for each character in T ; if character are same in both the string ; Driver Program
function CommomSubsequencesCount ( s , t ) { var n1 = s . length ; var n2 = t . length ; var dp = Array . from ( Array ( n1 + 1 ) , ( ) => Array ( n2 + 1 ) ) ; for ( var i = 0 ; i <= n1 ; i ++ ) { for ( var j = 0 ; j <= n2 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( var i = 1 ; i <= n1 ; i ++ ) { for ( var j = 1 ; j <= n2 ; j ++ ) { if ( s [ i - 1 ] == t [ j - 1 ] ) dp [ i ] [ j ] = 1 + dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ j ] ; else dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ j ] - dp [ i - 1 ] [ j - 1 ] ; } } return dp [ n1 ] [ n2 ] ; } var s = " " ; var t = " " ; document . write ( CommomSubsequencesCount ( s , t ) ) ;