Count the distinct subsequences of a string that equal a target string

Quick Overview

Count how many ways a target string can be formed as a subsequence of a source string, where each way is a different choice of index positions. It tests counting over string prefixes, picking the right base case, and keeping large intermediate counts correct.

Count the distinct subsequences of a string that equal a target string

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Given two strings `s` and `t`, return the number of distinct subsequences of `s` that equal `t`. A subsequence of a string is a new string formed by deleting some characters of the original (possibly none) without changing the relative order of the remaining characters. The test cases are generated so that the answer fits in a 32-bit signed integer. ### Function Signature ```python def num_distinct(s: str, t: str) -> int: ``` ### Rules - Count by index positions. Two ways are different when they keep a different set of positions in `s`, even though both spell `t`. - Characters are compared exactly, so `"a"` and `"A"` are different letters. - If `t` is longer than `s`, the answer is 0. ### Constraints - `1 <= len(s), len(t) <= 1000` - `s` and `t` consist of English letters. ### Examples ```text Input: s = "rabbbit", t = "rabbit" Output: 3 ``` `s` has three `b` characters at indices 2, 3 and 4, and `t` needs two of them. Every other letter of `t` has exactly one usable match, so the answer is the number of ways to keep two of the three: indices {2, 3}, {2, 4} or {3, 4}. ```text Input: s = "babgbag", t = "bag" Output: 5 ``` The increasing index triples for (b, a, g) are (0, 1, 3), (0, 1, 6), (0, 5, 6), (2, 5, 6) and (4, 5, 6). ```text Input: s = "ab", t = "ba" Output: 0 ``` The only `b` in `s` comes after the only `a`, so no subsequence of `s` reads `"ba"`.

Overview: Count how many ways a target string can be formed as a subsequence of a source string, where each way is a different choice of index positions. It tests counting over string prefixes, picking the right base case, and keeping large intermediate counts correct.

Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Given two strings s and t, return the number of distinct subsequences of s that equal t.

A subsequence of a string is a new string formed by deleting some characters of the original (possibly none) without changing the relative order of the remaining characters.

The test cases are generated so that the answer fits in a 32-bit signed integer.

Function Signature

def num_distinct(s: str, t: str) -> int:

Rules

  • Count by index positions. Two ways are different when they keep a different set of positions in s , even though both spell t .
  • Characters are compared exactly, so "a" and "A" are different letters.
  • If t is longer than s , the answer is 0.

Constraints

  • 1 <= len(s), len(t) <= 1000
  • s and t consist of English letters.

Examples

Input: s = "rabbbit", t = "rabbit"
Output: 3

s has three b characters at indices 2, 3 and 4, and t needs two of them. Every other letter of t has exactly one usable match, so the answer is the number of ways to keep two of the three: indices {2, 3}, {2, 4} or {3, 4}.

Input: s = "babgbag", t = "bag"
Output: 5

The increasing index triples for (b, a, g) are (0, 1, 3), (0, 1, 6), (0, 5, 6), (2, 5, 6) and (4, 5, 6).

Input: s = "ab", t = "ba"
Output: 0

The only b in s comes after the only a, so no subsequence of s reads "ba".