Count common subsequence in two strings | C # program to 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 code
using System ; class GFG { static int CommomSubsequencesCount ( string s , string t ) { int n1 = s . Length ; int n2 = t . Length ; int [ , ] dp = new int [ n1 + 1 , n2 + 1 ] ; for ( int i = 0 ; i <= n1 ; i ++ ) { for ( int j = 0 ; j <= n2 ; j ++ ) { dp [ i , j ] = 0 ; } } for ( int i = 1 ; i <= n1 ; i ++ ) { for ( int 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 ] ; } public static void Main ( ) { string s = " ajblqcpdz " ; string t = " aefcnbtdi " ; Console . Write ( CommomSubsequencesCount ( s , t ) ) ; } }