Data Structures & AlgorithmsHardTCS NQT · Infosys

How many pairs of positions i < j in the string aabbab have s[i] = a and s[j] = b (the letters need not be adjacent)?

  1. A. 7
  2. B. 8
  3. C. 9
  4. D. 11

Correct answer: A. 7

Each a contributes the number of b positions after it: the two a's at 0 and 1 see 3 b's each, and the a at 4 sees 1.

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

Exams that ask this topic