# Count common subsequence in two strings

Given two string **S** and **Q**. The task is to count the number of the common subsequence in S and T.

**Examples:**

Input : S = “ajblqcpdz”, T = “aefcnbtdi”

Output : 11

Common subsequences are : { “a”, “b”, “c”, “d”, “ab”, “bd”, “ad”, “ac”, “cd”, “abd”, “acd” }Input : S = “a”, T = “ab”

Output : 1

To find the number of common subsequences in two string, say S and T, we use Dynamic Programming by defining a 2D array **dp[][]**, where dp[i][j] is the number of common subsequences in the string S[0…i-1] and T[0….j-1].

Now, we can define dp[i][j] as

= dp[i][j-1] + dp[i-1][j] + 1, when S[i-1] is equal to T[j-1]

This is because when S[i-1] == S[j-1], using the above fact all the previous common sub-sequences are doubled as they get appended by one more character. Both dp[i][j-1] and dp[i-1][j] contain dp[i-1][j-1] and hence it gets added two times in our recurrence which takes care of doubling of count of all previous common sub-sequences. Addition of 1 in recurrence is for the latest character match : common sub-sequence made up of s1[i-1] and s2[j-1]

= dp[i-1][j] + dp[i][j-1] – dp[i-1][j-1], when S[i-1] is not equal to T[j-1]

Here we subtract dp[i-1][j-1] once because it is present in both dp[i][j – 1] and dp[i – 1][j] and gets added twice.

Below is the implementation of this approach:

## C++

`// C++ program to count common subsequence in two strings ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// return the number of common subsequence in ` `// two strings ` `int` `CommomSubsequencesCount(string s, string t) ` `{ ` ` ` `int` `n1 = s.length(); ` ` ` `int` `n2 = t.length(); ` ` ` `int` `dp[n1+1][n2+1]; ` ` ` ` ` `for` `(` `int` `i = 0; i <= n1; i++) { ` ` ` `for` `(` `int` `j = 0; j <= n2; j++) { ` ` ` `dp[i][j] = 0; ` ` ` `} ` ` ` `} ` ` ` ` ` `// for each character of S ` ` ` `for` `(` `int` `i = 1; i <= n1; i++) { ` ` ` ` ` `// for each charcater in T ` ` ` `for` `(` `int` `j = 1; j <= n2; j++) { ` ` ` ` ` `// if character are same in both ` ` ` `// the string ` ` ` `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]; ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `string s = ` `"ajblqcpdz"` `; ` ` ` `string t = ` `"aefcnbtdi"` `; ` ` ` ` ` `cout << CommomSubsequencesCount(s, t) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count common subsequence in two strings ` `public` `class` `GFG { ` ` ` ` ` `// return the number of common subsequence in ` ` ` `// two strings ` ` ` `static` `int` `CommomSubsequencesCount(String s, String t) ` ` ` `{ ` ` ` `int` `n1 = s.length(); ` ` ` `int` `n2 = t.length(); ` ` ` `int` `dp[][] = ` `new` `int` `[n1+` `1` `][n2+` `1` `]; ` ` ` `char` `ch1,ch2 ; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i <= n1; i++) { ` ` ` `for` `(` `int` `j = ` `0` `; j <= n2; j++) { ` ` ` `dp[i][j] = ` `0` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// for each character of S ` ` ` `for` `(` `int` `i = ` `1` `; i <= n1; i++) { ` ` ` ` ` `// for each character in T ` ` ` `for` `(` `int` `j = ` `1` `; j <= n2; j++) { ` ` ` ` ` `ch1 = s.charAt(i - ` `1` `); ` ` ` `ch2 = t.charAt(j - ` `1` `); ` ` ` ` ` `// if character are same in both ` ` ` `// the string ` ` ` `if` `(ch1 == ch2) ` ` ` `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]; ` ` ` `} ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String args[]){ ` ` ` ` ` `String s = ` `"ajblqcpdz"` `; ` ` ` `String t = ` `"aefcnbtdi"` `; ` ` ` ` ` `System.out.println(CommomSubsequencesCount(s, t)); ` ` ` ` ` `} ` ` ` `// This code is contributed by ANKITRAI1 ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count common ` `# subsequence in two strings ` ` ` `# return the number of common subsequence ` `# in two strings ` `def` `CommomSubsequencesCount(s, t): ` ` ` ` ` `n1 ` `=` `len` `(s) ` ` ` `n2 ` `=` `len` `(t) ` ` ` `dp ` `=` `[[` `0` `for` `i ` `in` `range` `(n2 ` `+` `1` `)] ` ` ` `for` `i ` `in` `range` `(n1 ` `+` `1` `)] ` ` ` ` ` `# for each character of S ` ` ` `for` `i ` `in` `range` `(` `1` `, n1 ` `+` `1` `): ` ` ` ` ` `# for each charcater in T ` ` ` `for` `j ` `in` `range` `(` `1` `, n2 ` `+` `1` `): ` ` ` ` ` `# if character are same in both ` ` ` `# the string ` ` ` `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] ` ` ` `# Driver Code ` `s ` `=` `"ajblqcpdz"` `t ` `=` `"aefcnbtdi"` ` ` `print` `(CommomSubsequencesCount(s, t)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count common ` `// subsequence in two strings ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// return the number of common ` `// subsequence in two strings ` `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 each character of S ` ` ` `for` `(` `int` `i = 1; i <= n1; i++) ` ` ` `{ ` ` ` ` ` `// for each character in T ` ` ` `for` `(` `int` `j = 1; j <= n2; j++) ` ` ` `{ ` ` ` ` ` `// if character are same in ` ` ` `// both the string ` ` ` `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]; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `string` `s = ` `"ajblqcpdz"` `; ` ` ` `string` `t = ` `"aefcnbtdi"` `; ` ` ` ` ` `Console.Write(CommomSubsequencesCount(s, t)); ` `} ` `} ` ` ` `// This code is contributed ` `// by ChitraNayal ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

11

**Time Complexity :** O(n1 * n2)

**Auxiliary Space : **O(n1 * n2)

**Source :** StackOverflow

## Recommended Posts:

- LCS (Longest Common Subsequence) of three strings
- Longest common anagram subsequence from N strings
- Count common characters in two strings
- Count the number of common divisors of the given strings
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Number of common base strings for two strings
- Longest Common Subsequence | DP-4
- Longest Common Subsequence with at most k changes allowed
- C++ Program for Longest Common Subsequence
- Longest Common Subsequence | DP using Memoization
- Printing Longest Common Subsequence
- Longest Common Increasing Subsequence (LCS + LIS)
- Longest Common Anagram Subsequence
- Length of longest common subsequence containing vowels
- Edit distance and LCS (Longest Common Subsequence)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.