Data Structures & AlgorithmsVery hard
In the 'shortest window with a then b in order' problem, why does taking the nearest later 'b' for each 'a' suffice?
- A. The nearest 'b' is always the first character of the string.
- B. Any later 'b' is at least as far as the nearest one, so the nearest later 'b' gives the shortest span for that 'a'.
- C. Shortest spans are always between adjacent letters.
- D. Only the last 'a' needs to be considered.
Correct answer: B. Any later 'b' is at least as far as the nearest one, so the nearest later 'b' gives the shortest span for that 'a'.
For a fixed starting 'a', the span grows with the position of the chosen 'b', so the nearest one is optimal. Minimizing over all 'a' then gives the global shortest span.
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
- Why is the maximum product of three values sometimes achieved by two large negatives and one large positive?
- For a binary string with no three equal consecutive bits, why is a run-length description convenient for counting?
- Why does the sliding-window approach fail to find the shortest subarray with sum divisible by m when values are NEGATIVE?
- Which property makes the ordered-pair count of A[i] > A[j] equal to the number of unequal unordered pairs?
- When an array's values are all distinct, what is the number of ordered pairs (i, j), i ≠ j, with A[i] > A[j]?
- Push and pop operations on an initially empty stack are: push, push, pop, push, push, push, pop, pop, push. What is the maximum height the…