Data Structures & AlgorithmsVery hardCodeforces Div2 D · GATE CS

How many DIFFERENT non-empty subsequences does the string "abcabc" have? Two subsequences are different if their letter sequences differ.

  1. A. 63
  2. B. 51
  3. C. 54
  4. D. 3

Correct answer: B. 51

Let dp[i] count distinct subsequences (including empty) of the first i letters: dp[i] = 2·dp[i−1] − dp[last−1], where last is the previous position of the same letter. Subtracting the duplicated ones is the step that the 2ⁿ − 1 count misses.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic