Tensorum
YouTubeसीखना शुरू करें
LeetCode 115 · LCS

Distinct Subsequences

LeetCode 115 · कठिन · समाधान, intuition और कदम-दर-कदम वॉकथ्रू

match ⇒ ways(i+1, j+1) + ways(i+1, j); else ways(i+1, j).

कठिनStringDynamic Programming

मैंने Distinct Subsequences (LeetCode 115) ऐसे हल किया — LCS पैटर्न का एक कठिन प्रॉब्लम। नीचे: intuition, कदम-दर-कदम approach, मेरा accepted Python कोड, animated test cases और line-by-line debugger।

नीचे का विवरण अंग्रेज़ी में है।

Intuition

Scan s and t together. When the characters match you have two choices: use this s-character for the current t-character, or skip it and hope to match later. When they don't match, you can only skip. Count every way that consumes all of t.

Approach

  1. 1

    If t is fully matched, that's one way.

  2. 2

    If s runs out first, zero ways.

  3. 3

    On a match, add 'use it' and 'skip it'; on a mismatch, only skip.

  4. 4

    Tabulation: the column for j = len(t) is all 1s, then fill from the end.

मैंने कैसे हल किया · पहले विचार से अंतिम हल तक

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

lru_cache dfs(index1, index2).

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

Count table filled from the end.

Recursion + memoPythonO(m·n) time · O(m·n) space
from functools import lru_cache
class Solution:
    def numDistinct(self, s: str, t: str) -> int:
        #case 1:
        #letters at current index are same
        #  a) if we move to next index for s and t we may get common letter/sequence
        #  b) if move to next index for s and compare with current letter at t, we may get another
        #case 2:
        #  letters at current index are not same
        #. a) we may get another common at next index for s
        #base case 
        #if we reached at end of s, check if all letters of t has been consumed, return 1 else 0
        #if we reached at end of t, return 1
        @lru_cache(maxsize=None)
        def dfs(index1,index2):
            if index1 == len(s):
                return 1 if index2==len(t) else 0
            if index2==len(t):
                return 1
            if s[index1]==t[index2]:
                return dfs(index1+1,index2+1)+dfs(index1+1,index2)
            else:
                return dfs(index1+1,index2)
        return dfs(0,0)
TabulationPythonO(m·n) time · O(m·n) space
class Solution:
    def numDistinct(self, s: str, t: str) -> int:
        m = len(s)
        n = len(t)
        dp = [[0]*(n+1) for _ in range(m+1)]
        for index1 in range(m+1):
            dp[index1][n]=1
        for i in range(m-1,-1,-1):
            for j in range(n-1,-1,-1):
                if s[i]==t[j]:
                    dp[i][j]= dp[i+1][j+1]+dp[i+1][j]
                else:
                    dp[i][j]=dp[i+1][j]
        return dp[0][0]
स्टेप-थ्रू लोड हो रहा है…

Distinct Subsequences — अक्सर पूछे जाने वाले प्रश्न

Distinct Subsequences के पीछे intuition क्या है?

Scan s and t together. When the characters match you have two choices: use this s-character for the current t-character, or skip it and hope to match later. When they don't match, you can only skip. Count every way that consumes all of t.

Distinct Subsequences (LeetCode 115) कैसे हल करें?

1. If t is fully matched, that's one way. 2. If s runs out first, zero ways. 3. On a match, add 'use it' and 'skip it'; on a mismatch, only skip. 4. Tabulation: the column for j = len(t) is all 1s, then fill from the end.

Distinct Subsequences की time और space complexity क्या है?

Time: O(m · n)। Space: O(m · n)। (Accepted approach: Tabulation।)

Distinct Subsequences में आम गलतियाँ क्या हैं?

Check 't finished' before 's finished' — an empty t matches in exactly one way. Counts can get large; Python ints are unbounded so no overflow here.

क्या Distinct Subsequences को हल करने के एक से ज़्यादा तरीके हैं?

Recursion + memo (O(m·n) time · O(m·n) space): lru_cache dfs(index1, index2). Tabulation (O(m·n) time · O(m·n) space): Count table filled from the end.