Data Structures & AlgorithmsHardGATE CS · TCS NQT
How many substrings of abbaba contain an equal number of letters a and b (substrings of length at least 2)?
- A. 8
- B. 9
- C. 11
- D. 7
Correct answer: D. 7
Map a to +1 and b to -1; a substring is balanced when its prefix sums match. Counting by prefix-sum equality is cleaner than enumerating substrings.
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
- Given the words ba, b and a, concatenate all three in some order. What is the lexicographically smallest resulting string?
- How many ways are there to write 10 as an ordered sum of 1s and 2s (for example 2+1+1+... counts differently from 1+2+...)?
- What is the multiplicative order of 2 modulo 17 (the smallest k > 0 with 2^k ≡ 1 mod 17)?
- How many integers x in [1, 100] can be written as a^2 + b^2 with positive integers a and b?
- What is 2^100 mod 7?
- Every element of the circular array [1, 2, 3, 4] is replaced by the sum of its two circular neighbours, taken modulo 10, simultaneously…