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.