3

There is a leetcode problem: distinct subsequences.

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).

Here is an example: S = "rabbbit", T = "rabbit" Return 3.


My questions:

Here, I don't understand what is the meaning of "count the number of distinct subsequences of T in S" I think "r", "ra","b" rab", "rabt" etc. are all subsequences of T, and they are also in S. But the return gives answer "3". So, I must have misunderstood the problem, could anyone explain it to me? Just giving me some more typical examples and explanations is OK and don't tell me how to solve it, I hope to do it as an exercise. Thanks in advance

  • Interpret the _number of distinct subsequences of T in S_ as: the number of distinct ways to form a subsequence of `S` that is equal to `T`. – Daniel Vérité Aug 02 '14 at 23:24
  • Really thanks for your explanation, I understand this problem now. – user3903015 Aug 02 '14 at 23:36
  • that is the weirdest interpretation of that sentence. it looks to me like it says: number of distinct subsequences of T in S. i.e. the number subsequences of T that are distinct and is contained in S. It looks to me like it should be "number of distinct subsequences of S that are equal to T". Where a distinct subsequence of S is defined as a tuple S and a list of characters to delete (as defined earlier in the problem). – thang Mar 14 '15 at 04:12

2 Answers2

0

You can get T="rabbit" from S="rabbbit" by deleting either the first, the second, or the third "b" in S. Hence, the number of possible variants is 3.

Ashalynd
  • 12,363
  • 2
  • 34
  • 37
0

I think you misunderstood the question. Count the number of distinct sub-sequences of T in S means how many unique appearances of T (rabbit) are there in S (rabbbit).

The answer is three:

(The bold letter being the deleted one)

r a b b b i t == rabbit

r a b b b i t == rabbit

r a b b b i t == rabbit

See? Three distinct sub-sequences of the word "rabbit" taken the string "rabbbit". A different character was deleted each time.

gnarlybracket
  • 1,691
  • 4
  • 18
  • 37