π One-stop destination for all your technical interview Preparation π
Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.
For example, βaceβ is a subsequence of βabcdeβ. A common subsequence of two strings is a subsequence that is common to both strings.
class Solution {
private:
int lcsHelper(string s1, string s2, int i, int j)
{
if (i == 0 || j == 0) return 0;
if (s1[i - 1] == s2[j - 1]) return 1 + lcsHelper(s1, s2, i - 1, j - 1);
return max(lcsHelper(s1, s2, i - 1, j), lcsHelper(s1, s2, i, j - 1));
}
public:
int longestCommonSubsequence(string text1, string text2)
{
int n = text1.size();
int m = text2.size();
return lcsHelper(text1, text2, n, m);
}
};
class Solution {
int memo[1001][1001];
private:
int lcsHelper(string& s1, string& s2, int i, int j)
{
if (i == 0 || j == 0) return 0;
if (memo[i][j] != -1) return memo[i][j];
if (s1[i - 1] == s2[j - 1]) return memo[i][j] = 1 + lcsHelper(s1, s2, i - 1, j - 1);
return memo[i][j] = max(lcsHelper(s1, s2, i - 1, j), lcsHelper(s1, s2, i, j - 1));
}
public:
int longestCommonSubsequence(string text1, string text2)
{
int n = text1.size();
int m = text2.size();
memset(memo, -1, sizeof(memo));
return lcsHelper(text1, text2, n, m);
}
};
class Solution {
public:
int longestCommonSubsequence(string text1, string text2)
{
int n = text1.size(), m = text2.size();
int dp[n + 1][m + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
if (i == 0 || j == 0)
dp[i][j] = 0;
else if (text1[i - 1] == text2[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[n][m];
}
};