Facebook Pixel

Distinct Subsequences

Problem Statement

Given two strings s and t, count how many distinct subsequences of s equal t.

s = "rabbbit"
t = "rabbit"

Answer: 3

The string s has three 'b's. We can form "rabbit" by choosing any two of them:

r a b b b i t
    ^-^       → "rabbit" (use 1st and 2nd b)
    ^---^     → "rabbit" (use 1st and 3rd b)
      ^-^     → "rabbit" (use 2nd and 3rd b)
Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro