LC115 - Distinct Subsequences
Problem
Example
Solution
Cached DFS
def numDistinct(self, s: str, t: str) -> int:
@cache
def dfs(i, j):
# end reached, 1 distinct value found
if j == len(t):
return 1
# end of s reached without reaching t, no distinct value found
if i == len(s):
return 0
tmp = dfs(i+1)
if s[i] == t[j]:
tmp += dfs(i+1, j+1)
return tmp
return dfs(0,0)Last updated