
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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.
Example
#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]; } else{ 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; }
Example
If we run the above code we will get the following output −
count is: 51
- Related Articles
- Count common characters in two strings in C++
- Common Character Count in Strings in JavaScript
- Common Words in Two Strings in Python
- Program to find length of longest common subsequence of three strings in Python
- Longest Common Subsequence in C++
- Longest Common Subsequence
- Count the number of common divisors of the given strings in C++
- Print common characters of two Strings in alphabetical order in C++
- Count common prime factors of two numbers in C++
- Function to check two strings and return common words in JavaScript
- Finding the longest common consecutive substring between two strings in JavaScript
- Python program to remove words that are common in two Strings
- C++ Program for Longest Common Subsequence
- Java Program for Longest Common Subsequence
- Python code to print common characters of two Strings in alphabetical order
