Distinct Subsequences
LeetCode 115 · కఠినం · పరిష్కారం, intuition & దశలవారీ వాక్త్రూ
match ⇒ ways(i+1, j+1) + ways(i+1, j); else ways(i+1, j).
నేను 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
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.
నేను ఎలా పరిష్కరించాను · మొదటి ఆలోచన నుంచి చివరి పరిష్కారం వరకు
lru_cache dfs(index1, index2).
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.