Data Structures & AlgorithmsVery hard
In "xaybzaxxbab", what is the length of the shortest contiguous substring that contains the letter 'a' followed later by the letter 'b' (the two need not be adjacent)?
- A. 1
- B. 3
- C. 2
- D. 11
Correct answer: C. 2
For each 'a' take the nearest later 'b'; the shortest such span is the minimum of these distances plus one. A two-pointer or stack sweep finds the same minimum in linear time.
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
- How many contiguous subarrays of 4 2 7 1 5 3 6 of length at least 2 have their FIRST element equal to their minimum?
- How many contiguous subarrays of 1 2 4 3 5 2 have XOR equal to their SUM?
- What is the length of the shortest contiguous subarray of 1 4 2 7 3 5 6 1 whose sum is at least 14?
- How many contiguous subarrays of 2 3 4 5 6 7 8 contain EXACTLY two even values and have an ODD length?
- How many index pairs i < j in 1 3 6 5 4 10 2 have A[i] + A[j] equal to a perfect square?
- Choose three DIFFERENT positions of -10 -3 1 2 7 -1 to maximise the product of their values. What is the maximum product?