
g1101_1200.s1143_longest_common_subsequence.Solution.cs 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
namespace LeetCodeNet.G1101_1200.S1143_longest_common_subsequence {
// #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_01_07_Time_59_ms_(90.45%)_Space_41.3_MB_(54.32%)
public class Solution {
public int LongestCommonSubsequence(string text1, string text2) {
int n = text1.Length;
int m = text2.Length;
int[,] dp = new int[n + 1, m + 1];
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] = Math.Max(dp[i - 1, j], dp[i, j - 1]);
}
}
}
return dp[n, m];
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy