Question 4
Program to count common subsequence in two strings
Today we will learn program to count common subsequence in two strings. We will take two strings as an input, then we will 2-D ”cnt[][]” array that will store the count of common subsequence found. Now we will iterate each character of first string and each character of second string from of the string to its end , then if the characters matches we will check if the previous character of both string are same or not if they are same we will assign ”1 +cnt[i][j – 1] +cnt[i – 1][j]” to our 2D else we will assign ”cnt[i][j – 1] + cnt[i – 1][j] – cnt[i – 1][j – 1]” to our 2D array. As the iteration ends we will get our count.
Program to count common subsequence in two strings
C
C++
C
#include <stdio.h> #include <string.h> //Function to count the number of subsequences in the string. int countsequences(char str[], char str2[]) { int n1 = strlen(str); int n2 = strlen(str2); int cnt[n1+1][n2+1]; for (int i = 0; i <= n1; i++) { for (int j = 0; j <= n2; j++) { cnt[i][j] = 0; } } //Taking each character from first string. for (int i = 1; i <= n1; i++) { //Taking each character from second string. for (int j = 1; j <= n2; j++) { //If characters are same in both the string. if(str[i-1] == str2[j-1]) { cnt[i][j] = 1 + cnt[i][j-1] + cnt[i-1][j]; } else { cnt[i][j] = cnt[i][j-1] + cnt[i-1][j] - cnt[i-1][j-1]; } } } return cnt[n1][n2]; } //Main function for printing the result. int main() { char str[100] ,str2[100]; printf("Enter the first string: "); gets(str); printf("Enter string second: "); gets(str2); printf("Number of common subsequence is: %d ",countsequences(str, str2)); return 0; }
Output: Enter the first string: abc Enter string second: bc Number of common subsequence is: 3
C++
#include <iostream> #include <string.h> using namespace std; //Function to count the number of subsequences in the string int countsequences(char str[], char str1[]) { int l1 = strlen(str); int l2 = strlen(str1); int cnt[l1+1][l2+1]; for (int i = 0; i <= l1; i++) { for (int j = 0; j <= l2; j++) { cnt[i][j] = 0; } } //Taking each character from first string for (int i = 1; i <= l1; i++) { //Taking each character from second string for (int j = 1; j <= l2; j++) { //If characters are same in both the string if(str[i-1] == str1[j-1]) { cnt[i][j] = 1 + cnt[i][j-1] + cnt[i-1][j]; } else { cnt[i][j] = cnt[i][j-1] + cnt[i-1][j] - cnt[i-1][j-1]; } } } return cnt[l1][l2]; } //Main function for printing the result int main() { char str[100] ,str1[100]; cout<<"Enter the first string: "; cin>>str; cout<<"Enter the second string: "; cin>>str1; cout<<"Number of common subsequence is: "<<countsequences(str, str1); return 0; }
Output: Enter the first string: abc Enter the second string: ab Number of common subsequence is: 3
JAVA Solution
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
String str1 = scn.next();
String str2 = scn.next();
ArrayList res = getSubSequence(str1);
ArrayList res1 = getSubSequence(str2);
int len = 0;
ArrayList temp = new ArrayList();
ArrayList large = new ArrayList();
if(res.size()>res1.size()){
len = res1.size();
temp = res1;
large = res;
}else {
len = res.size();
temp = res;
large = res1;
}
int count=0;
for(int i=0;i<len;i++){
String s = temp.get(i);
for(int j=0;j<large.size();j++){
if(s.equals(large.get(j))){
count++;
}
}
}
System.out.print(count-1);
}
public static ArrayList getSubSequence(String str){
if(str.length()==0){
ArrayList bres = new ArrayList();
bres.add(“”);
return bres;
}
char ch = str.charAt(0);
String subs = str.substring(1);
ArrayList rres = getSubSequence(subs);
ArrayList nres = new ArrayList();
for(String s: rres){
nres.add(“”+s);
}
for(String s : rres){
nres.add(ch+s);
}
return nres;
}
}
Python Code:
def longcomsub(str1,str2):
n1 = len(str1)
n2 = len(str2)
c = []
for i in range(0,n1+1):
temp = []
for j in range(0,n2+1):
temp.append(0)
c.append(temp)
for i in range(1,n1+1):
for j in range(1,n2+1):
if str1[i-1] == str2[j-1]:
# c[i][j] = 1 + c[i-1][j-1]
c[i][j] = 1 + c[i-1][j] + c[i][j-1]
else:
c[i][j] = c[i-1][j] + c[i][j-1] – c[i-1][j-1]
return c[n1][n2]
str1 = “abc”
str2 = “bc”
print(longcomsub(str1,str2))
#Common subsequences in two given strings
str1=input()
str2=input()
n1,n2=len(str1),len(str2)
sorted1=sorted(str1)
sorted2=sorted(str2)
temp=[]
for i in sorted1:
for _ in range(n2):
if i in sorted2:
temp.append(i)
else:
continue
temp=sorted(list(set(temp)))
temp1=temp
temp2=[]
for i in range(len(temp)):
for j in range(i+1,len(temp)):
try:
temp2.append(temp[i]+temp[j])
except:
pass
for i in temp2:
temp.append(i)
var=”.join(temp1)
temp.append(var)
temp=list(set(temp))
result=len(temp)
print(result)
#include
using namespace std;
int main()
{
int i,j;
string s,t;
cin>>s>>t;
const int len1=s.length();
const int len2=t.length();
int dp[len1+1][len2+1];
for(i=0;i<len1+1;i++)
{
for(j=0;j<len2+1;j++)
{
if(i==0||j==0)
{
dp[i][j]=0;
}
else
{
if(s[i-1]==t[j-1])
{
dp[i][j]=dp[i-1][j]+dp[i][j-1]+1;
}
else
{
dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1];
}
}
}
}
cout<<dp[len1][len2]<<endl;
return 0;
}
s1=’gouragv’
s2=’raghuv’
dp=[[0 for i in range (len(s2)+1)] for i in range (len(s1)+1)]
for i in range (1,len(s1)+1):
for j in range (1,len(s2)+1):
if(s1[i-1]==s2[j-1]):
dp[i][j]=1+dp[i-1][j-1]
else:
dp[i][j]=max(dp[i-1][j],dp[i][j-1])
print(dp[len(s1)][len(s2)])
package com.code4you.common_subsequence;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter first string:\n”);
String str1 = sc.nextLine();
System.out.println(“Enter second string:\n”);
String str2 = sc.nextLine();
sc.close();
countCommonSubsequence(str1,str2);
}
private static void countCommonSubsequence(String str1, String str2) {
int str1Len = str1.length();
int str2Len = str2.length();
int[][] countArr = new int[str1Len+1][str2Len+1];
for (int i = 1; i <= str1Len; i++) {
for (int j = 1; j <= str2Len; j++) {
if (str1.charAt(i-1) == str2.charAt(j-1)) {
countArr[i][j] = 1+countArr[i-1][j]+countArr[i][j-1];
}else{
countArr[i][j] = countArr[i-1][j]+countArr[i][j-1]-countArr[i-1][j-1];
}
}
}
for (int i = 0; i < str1Len; i++) {
for (int j = 0; j < str2Len; j++) {
System.out.print(countArr[i][j]+" ");
}
System.out.println();
}
System.out.println(countArr[str1Len][str2Len]);
}
}
Nice
where is the question ?
its a blank page here !
If you have any query, kindly Whatsapp on the given number, 9711936107
more questions related to advance coding
Nice
More interested to know the questions.
k