Count common subsequence in two strings in C++

We are given two strings, let's say str1 and str2 containing characters and the task is to calculate the common subsequences in both the strings. In the below program we are using dynamic programming and for that we need to know what dynamic programming is and at what problems it can be used.

Dynamic programming approach is similar to divide and conquer in breaking down the problem into smaller and yet smaller possible sub-problems. But unlike, divide and conquer, these subproblems are not solved independently. Rather, results of these smaller subproblems are remembered and used for similar or overlapping sub-problems.

Dynamic programming is used where we have problems, which can be divided into similar subproblems, so that their results can be reused. Mostly, these algorithms are used for optimization. Before solving the in-hand sub-problem, dynamic algorithms will try to examine the results of the previously solved sub-problems. The solutions of sub-problems are combined in order to achieve the best solution.

So we can say that −

Input − string str1 = “abc”
      String str2 = “ab”
Output − count is 3

Explanation − From the given strings common subsequences formed are: {‘a’, ‘b’ , ‘ab’}.

Input − string str1 = “ajblqcpdz”
      String str2 = “aefcnbtdi”
Output − count is 11

Common subsequences are − From the given strings common subsequences formed are: { “a”, “b”, “c”, “d”, “ab”, “bd”, “ad”, “ac”, “cd”, “abd”, “acd” }

Approach used in the below program is as follows

  • Input the two strings let’s say str1 and str2.

  • Calculate the length of the given string using the length() function that will return an integer value as per the number of characters in a string and store it in len1 for str1 and in len2 for str2.

  • Create a 2-D array to implement dynamic programming let’s say arr[len1+1][len2+1]

  • Start loop for i to 0 till i less than len1

  • Inside loop, start another loop for j to 0 till j less than len2

  • Inside loop, check IF str1[i-1] = str2[j-1] then set arr[i][j] = 1 + arr[i][j-1] + arr[i-1][j]

  • Else, then set arr[i][j] = arr[i][j-1] + arr[i-1][j] = arr[i-1][j-1]

  • Return arr[len1][len2]

  • Print the result.


 Live Demo

#include <iostream>
using namespace std;
// To count the number of subsequences in the string
int countsequences(string str, string str2){
   int n1 = str.length();
   int n2 = str2.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 str
   for (int i = 1; i <= n1; i++){
      // for each character in str2
      for (int j = 1; j <= n2; j++){
         // if character are same in both
         // the string
         if (str[i - 1] == str2[j - 1]){
            dp[i][j] = 1 + dp[i][j - 1] + dp[i - 1][j];
            dp[i][j] = dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1];
   return dp[n1][n2];
int main(){
   string str = "abcdejkil";
   string str2 = "bcdfkaoenlp";
   cout <<"count is: "<<countsequences(str, str2) << endl;
   return 0;


If we run the above code we will get the following output −

count is: 51