Tensorum
LeetCode 115 · LCS

Distinct Subsequences

LeetCode 115 · Hard · solution, intuition & step-by-step walkthrough

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

HardStringDynamic Programming
369 msruntime · beats 61%
75.6 MBmemory · beats 44%
2approaches, in order
15accepted runs · last 9 Mar 2026

This is how I solved Distinct Subsequences (LeetCode 115), a hard problem from the LCS pattern. Below: the intuition, the approach step by step, my accepted Python code and the 2 versions I wrote on the way, animated test cases and a 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.

How I solved it · from first idea to final

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]
Loading the step-through…

Distinct Subsequences — FAQ

What is the intuition behind Distinct Subsequences?

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.

How do you solve 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.

What is the time and space complexity of Distinct Subsequences?

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

What are common mistakes in 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.

Is there more than one way to solve 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.