-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterleaving_strings.cpp
52 lines (52 loc) · 1.3 KB
/
interleaving_strings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Problem Link - https://www.interviewbit.com/problems/interleaving-strings/
/* Problem Solution Function
int lcs(string A, string B)
{
int a = A.size();
int b = B.size();
vector<vector<int>> dp(a+1, vector<int> (b+1));
for(int i=0; i<=a; i++)
{
for(int j=0; j<=b; j++)
{
if(i==0 || j==0) dp[i][j] = 0;
else if(A[i-1] == B[j-1]) dp[i][j] = 1+dp[i-1][j-1];
else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
return dp[a][b];
}
int Solution::isInterleave(string A, string B, string C) {
int a = lcs(A, C);
int b = lcs(B,C);
if(a == A.size() && b == B.size()) return 1;
return 0;
}
*/
#include <bits/stdc++.h>
using namespace std;
int lcs(string A, string B)
{
int a = A.size();
int b = B.size();
vector<vector<int>> dp(a+1, vector<int> (b+1));
for(int i=0; i<=a; i++)
{
for(int j=0; j<=b; j++)
{
if(i==0 || j==0) dp[i][j] = 0;
else if(A[i-1] == B[j-1]) dp[i][j] = 1+dp[i-1][j-1];
else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
return dp[a][b];
}
int main(){
string A = "aabcc"
string B = "dbbca"
string C = "aadbbcbcac"
int a = lcs(A, C);
int b = lcs(B,C);
if(a == A.size() && b == B.size()) return 1;
return 0;
}