Data Structures & AlgorithmsModerateGATE CS · ICPC regional
How many substrings of the string aabbaabb have an equal number of letters a and b?
- A. 11
- B. 12
- C. 14
- D. 10
Correct answer: D. 10
Map a to +1 and b to -1; a substring is balanced when two prefix sums are equal. Counting prefix-sum repeats finds all balanced substrings in O(n) with a hash map.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is the length of the longest prefix of abacaba that is a palindrome?
- What is the sum of floor(i / 2) for i = 1 to 10?
- What is the smallest positive integer that has exactly 6 positive divisors?
- How many integers n in [1, 100] have both n and n + 2 prime?
- What is gcd(2^12 - 1, 2^8 - 1)?
- How many integers in [2, 20] are neither divisible by 2 nor by 3?