Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT
How many substrings of "abcabcb" contain at least one each of the letters a, b and c?
- A. 18
- B. 11
- C. 14
- D. 28
Correct answer: C. 14
Count with a sliding window: for each left end, the smallest right end where all three letters appear determines how many right ends are valid (n − right + 1). Counting only substrings of length three misses longer valid ones.
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 shortest substring of "aabcbcdbca" that contains every distinct letter appearing in the whole string at least…
- How many contiguous subarrays of [4, 2, 3, 1, 6, 5, 7, 9] satisfy max − min = (length of the subarray) − 1? Count every such subarray…
- What is the length of the longest substring of "bbaacabaacb" that contains EXACTLY two letters 'a'?
- Choose exactly 4 elements of [6, 1, 9, 4, 7, 2, 8, 5] (by position) such that no two chosen positions are adjacent. What is the maximum…
- How many permutations of 1, 2, 3, 4, 5 have exactly 3 inversions (pairs of positions i < j with p[i] > p[j])?
- What is the minimum number of positive perfect cubes (1, 8, 27, ...) whose sum is exactly 31? Repeats are allowed.