Longest Common Subsequence

Length of the LCS of two strings — 2D DP rolling over rows.

Medium
Time O(n * m) Space O(m)
LeetCode
3 min read
dp string

Problem#

Return the length of the longest common subsequence of text1 and text2.

Examples#

  • text1 = "abcde", text2 = "ace"3

Constraints#

  • 1 <= n, m <= 1000.

Approach#

dp[i][j] = LCS of text1[:i] and text2[:j]. Recurrence: if last chars match, dp[i][j] = dp[i-1][j-1] + 1; else max of dropping one char. Rolling 1D array saves space.

Solution#

Longest Common Subsequence
class Solution {
public:
int longestCommonSubsequence(string a, string b) {
int n = a.size(), m = b.size();
vector<int> dp(m + 1, 0);
for (int i = 1; i <= n; ++i) {
int prev = 0;
for (int j = 1; j <= m; ++j) {
int temp = dp[j];
if (a[i - 1] == b[j - 1]) dp[j] = prev + 1;
else dp[j] = max(dp[j], dp[j - 1]);
prev = temp;
}
}
return dp[m];
}
};

Editorial#

prev holds the previous row’s j-1 value (the diagonal predecessor). Rolling 1D saves O(n) memory vs the full 2D array. The recurrence is the canonical edit-distance family.

Complexity#

  • Time: O(n * m).
  • Space: O(m).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.