Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Subsequence Count

Learn
Play
Prove
Problem context and objectives
Mission briefing

Dungeon strategy engineer · A robot must cross a tiled dungeon

Store enough solved tile states to plan the complete route: The Subsequence Count.

A greedy step can trap the robot; recomputing every route is too expensive.

How you win

  1. 1Recognize when Distinct subsequences matches the clues
  2. 2Keep this true after every move: dp[j] counts ways the processed source prefix forms the first j target characters
  3. 3Reach the result within O(mn) time and O(n) space

Rules and pressure

  • Target cost: O(mn) time and O(n) space
  • State rule: dp[j] counts ways the processed source prefix forms the first j target characters
Lesson 1 of 3

Live algorithm trace

Distinct subsequences

Complete execution
1 of 5
r
source
a
b
b
b
i

Seed the empty target with one alignment.

1dp[0] = 1
2scan source characters
3scan target backward
4if characters match
5dp[j + 1] += dp[j]
6return dp[target length]
source = rabbbittarget = rabbitdp0 = 1
Truth to preserve / Cost target
Truth to preserve

dp[j] counts ways the processed source prefix forms the first j target characters

Cost target

O(mn) time and O(n) space

A source character may be skipped, or if it matches the next target character, used to extend every prior alignment. Reverse target iteration prevents reusing one source character twice.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead