Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 C

Problem: How many subsequences equal to the string "abc" are contained in "aabbcbcac" (a subsequence picks positions in increasing order, not necessarily contiguous)?

  1. A. 27
  2. B. 6
  3. C. 16
  4. D. 18

Correct answer: C. 16

Scan once, adding count('ab' so far) at each c and count('a' so far) at each b: the c's at positions 4, 6 and 8 contribute 4 + 6 + 6 = 16. Multiplying the letter counts 3 × 3 × 3 ignores order (27), counting 'ab' alone gives 6, and multiplying all 'ab' pairs by the 3 c's ignores that each c must come after its b (18).

More Coding Interview Problems questions

Exams that ask this topic