Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: How many distinct subsequences (as distinct strings, including the empty string) does "aba" have?

  1. A. 8
  2. B. 6
  3. C. 3
  4. D. 7

Correct answer: D. 7

The distinct subsequences of 'aba' are '', a, b, ab, ba, aa and aba: 7 (the DP doubles per character and subtracts the count before the previous same letter). Doubling without subtracting gives 8, excluding the empty string gives 6, and listing only the length-2 subsequences (ab, ba, aa) gives 3.

More Coding Interview Problems questions

Exams that ask this topic