
g1101_1200.s1143_longest_common_subsequence.Solution.cpp Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
The newest version!
// #Medium #Top_100_Liked_Questions #String #Dynamic_Programming
// #Algorithm_II_Day_17_Dynamic_Programming #Dynamic_Programming_I_Day_19
// #Udemy_Dynamic_Programming #Big_O_Time_O(n*m)_Space_O(n*m)
// #2024_05_21_Time_23_ms_(62.70%)_Space_25.6_MB_(50.39%)
#include
#include
#include
class Solution {
public:
int longestCommonSubsequence(const std::string& text1, const std::string& text2) {
int n = text1.size();
int m = text2.size();
std::vector> dp(n + 1, std::vector(m + 1, 0));
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (text1[i - 1] == text2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[n][m];
}
};
© 2015 - 2025 Weber Informatics LLC | Privacy Policy