-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubsequence.cpp
More file actions
50 lines (47 loc) · 1.35 KB
/
LongestCommonSubsequence.cpp
File metadata and controls
50 lines (47 loc) · 1.35 KB
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
#include <iostream>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
class Solution
{
private:
// ! memo table as a 2d vector to store computed results
vector<vector<int>> memo;
int lcs(const string &text1, const string &text2, int i, int j)
{
// adding a base case (as for the recursive approach)
if (i == text1.size() || j == text2.size())
{
return 0;
}
// check for precomputed values in memo
if (memo[i][j] != -1) // means that if they hold values (it has been used , so retain the values inside)
{
return memo[i][j];
}
if (text1[i] == text2[j]) //! match
{
// if both letters are same in both strings (then add 1 and the func recursive call)
memo[i][j] = 1 + lcs(text1, text2, i + 1, j + 1);
return memo[i][j];
}
//! no match
int first_skip = lcs(text1, text2, i + 1, j);
int second_skip = lcs(text1, text2, i, j + 1);
memo[i][j] = max(first_skip, second_skip);
return memo[i][j];
}
public:
int longestCommonSubsequence(string text1, string text2)
{
// initialize the memo
//! assinging a number of vectors of a fixed size inside memo and their count is the same size of string text1 param
memo.assign(text1.length(), vector<int>(text2.length(), -1));
return lcs(text1, text2, 0, 0);
}
};
int main()
{
return 0;
}