Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 1143 · LCS

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)).

நடுத்தரம்StringDynamic ProgrammingLongest Common Subsequence

நான் 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. 1

    Top-down lcs(i, j) with lru_cache; either string exhausted → 0.

  2. 2

    Bottom-up: fill dp from the end of both strings back to (0, 0).

  3. 3

    Each row only needs the row below it, so the final version keeps prev and cur arrays.

நான் எப்படித் தீர்த்தேன் · முதல் யோசனையிலிருந்து இறுதித் தீர்வு வரை

1
Recursion + memoO(m·n) time · O(m·n) space

lru_cache lcs(i, j).

2
TabulationO(m·n) time · O(m·n) space

2-D table from the end.

3
Space-optimised rowsAcceptedO(m·n) time · O(n) space

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.