Data Structures & AlgorithmsModerate
What is the length of the longest contiguous substring of "xxyxyyxyxxy" in which the number of 'x' letters is at most the number of 'y' letters?
- A. 9
- B. 21
- C. 10
- D. 11
Correct answer: C. 10
Map 'y' to +1 and 'x' to −1; the condition is a non-negative balance, which is answered with prefix sums and first occurrences of each prefix value. The strict version and the full string are different quantities.
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
- Sort 4 7 4 2 7 2 9 4 3 so that values with higher FREQUENCY come first; values with equal frequency are ordered by value ascending, and…
- In the sorted array 1 1 1 2 2 3 3 3 3 4, remove the extra copies so that every value appears AT MOST TWICE, keeping the order. What is the…
- In the string "abciiidefoeuxyz", what is the largest number of vowels (a, e, i, o, u) that appear in any contiguous window of length 3?
- What is the length of the longest contiguous substring of "aabbccabcdd" in which EVERY distinct letter appears at least twice?
- How many contiguous windows of length exactly 5 in 1 2 3 4 5 6 7 8 9 10 2 have a PRIME sum?
- How many contiguous subarrays of 2 4 6 4 2 8 have an AVERAGE exactly equal to the average of the whole array?