Data Structures & AlgorithmsHard

In the string "aabbaab", how many pairs of positions i < j form the subsequence 'ab' (S[i] = 'a' and S[j] = 'b')?

  1. A. 8
  2. B. 2
  3. C. 12
  4. D. 4

Correct answer: A. 8

Count the index pairs in order: every 'a' before a 'b' contributes one subsequence. Counting only adjacent 'ab' substrings, multiplying the letter totals ignores order, and reversing the roles counts 'ba' pairs.

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

Exams that ask this topic