Data Structures & AlgorithmsModerateTCS NQT · Infosys

In the string "abcabcaab", count the non-empty substrings that contain equally many vowels and consonants (vowels are a, e, i, o, u).

  1. A. 15
  2. B. 11
  3. C. 5
  4. D. 12

Correct answer: D. 12

Map a vowel to +1 and a consonant to -1 and count substrings whose prefix balance returns to the same value. The count is a pure prefix-equality count, so a hash map over balances solves it in linear time.

More Coding Interview Problems questions

Exams that ask this topic