Longest Common Subsequence
LeetCode 1143 · మధ్యస్థం · పరిష్కారం, intuition & దశలవారీ వాక్త్రూ
lcs(i, j) = 1 + lcs(i+1, j+1) on a match, else max(lcs(i+1, j), lcs(i, j+1)).
నేను Longest Common Subsequence (LeetCode 1143) ను ఇలా పరిష్కరించాను — LCS ప్యాటర్న్లోని ఒక మధ్యస్థం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.
కింది వివరణ ఇంగ్లీష్లో ఉంది.
Intuition
Compare the current characters of both strings. If they match, they can both be part of the subsequence: take 1 and move both pointers. If not, one of them is not in the answer — drop it from one side or the other and keep the better result.
Approach
- 1
Top-down lcs(i, j) with lru_cache; either string exhausted → 0.
- 2
Bottom-up: fill dp from the end of both strings back to (0, 0).
- 3
Each row only needs the row below it, so the final version keeps prev and cur arrays.
నేను ఎలా పరిష్కరించాను · మొదటి ఆలోచన నుంచి చివరి పరిష్కారం వరకు
lru_cache lcs(i, j).
2-D table from the end.
prev / cur rolling rows.
Recursion + memoPythonO(m·n) time · O(m·n) space
from functools import lru_cache
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
#case 1:
#letters are correct index are same
#1+lcs(i+1,j+1)
#case 2
#letters at current index are not same
#max(lcs(i+1,j),lcs(i,j+1))
#base case if any of the string is out of index return 0
@lru_cache(maxsize=None)
def lcs(i,j):
if i==len(text1) or j == len(text2):
return 0
if text1[i]==text2[j]:
return 1+lcs(i+1,j+1)
else:
return max(lcs(i+1,j),lcs(i,j+1))
return lcs(0,0)TabulationPythonO(m·n) time · O(m·n) space
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
#case 1:
#letters are correct index are same
#1+lcs(i+1,j+1)
#case 2
#letters at current index are not same
#max(lcs(i+1,j),lcs(i,j+1))
#base case if any of the string is out of index return 0
m = len(text1)
n = len(text2)
dp = [[0]*(n+1) for _ in range(m+1)]
for i in range(m-1,-1,-1):
for j in range(n-1,-1,-1):
if text1[i]==text2[j]:
dp[i][j]=1+dp[i+1][j+1]
else:
dp[i][j]=max(dp[i+1][j],dp[i][j+1])
return dp[0][0]Space-optimised rowsPythonO(m·n) time · O(n) space
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
#case 1:
#letters are correct index are same
#1+lcs(i+1,j+1)
#case 2
#letters at current index are not same
#max(lcs(i+1,j),lcs(i,j+1))
#base case if any of the string is out of index return 0
m = len(text1)
n = len(text2)
prev = [0]*(n+1)
cur = [0]*(n+1)
for i in range(m-1,-1,-1):
for j in range(n-1,-1,-1):
if text1[i]==text2[j]:
cur[j]=1+prev[j+1]
else:
cur[j]=max(prev[j],cur[j+1])
prev=cur[:]
return prev[0]Longest Common Subsequence — తరచూ అడిగే ప్రశ్నలు
Longest Common Subsequence వెనుక ఉన్న intuition ఏమిటి?
Compare the current characters of both strings. If they match, they can both be part of the subsequence: take 1 and move both pointers. If not, one of them is not in the answer — drop it from one side or the other and keep the better result.
Longest Common Subsequence (LeetCode 1143) ను ఎలా పరిష్కరించాలి?
1. Top-down lcs(i, j) with lru_cache; either string exhausted → 0. 2. Bottom-up: fill dp from the end of both strings back to (0, 0). 3. Each row only needs the row below it, so the final version keeps prev and cur arrays.
Longest Common Subsequence యొక్క time, space complexity ఎంత?
Time: O(m · n). Space: O(n) in the final version. (Accepted approach: Space-optimised rows.)
Longest Common Subsequence లో సాధారణ పొరపాట్లు ఏమిటి?
Subsequence ≠ substring — characters don't have to be contiguous. Copy cur into prev each row (prev = cur[:]).
Longest Common Subsequence ను పరిష్కరించడానికి ఒకటి కంటే ఎక్కువ మార్గాలు ఉన్నాయా?
Recursion + memo (O(m·n) time · O(m·n) space): lru_cache lcs(i, j). Tabulation (O(m·n) time · O(m·n) space): 2-D table from the end. Space-optimised rows (O(m·n) time · O(n) space): prev / cur rolling rows.