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
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು 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.