Practice
p in s. What is the final returned list when s = "cbaebabacd" and p = "abc"?Solution
Step 1: Trace first window (indices 0 to 2)
Substring "cba" is an anagram of "abc", so index 0 is added.Step 2: Slide window and check matches
Next valid anagram found at index 6 ("bac"). No other windows match all counts.Final Answer:
Option D -> Option DQuick Check:
Known example output matches [0, 6] [OK]
- Including indices where substring is not full anagram
- Off-by-one errors in window boundaries
k in an array of length n?Solution
Step 1: Analyze outer and inner loops
The right pointer moves from 0 to n-1 once. The left pointer only moves forward when product ≥ k, also at most n times.Step 2: Confirm total operations
Each element is visited at most twice (once by right, once by left), so total operations are O(n).Final Answer:
Option B -> Option BQuick Check:
Two pointers each move forward at most n times -> O(n) [OK]
- Assuming inner loop runs n times per iteration -> O(n²)
- Confusing division cost as log n
Solution
Step 1: Understand reuse impact
When characters can be reused infinitely, tracking last seen indices is insufficient; frequency counts are needed.Step 2: Modify approach
Use a frequency map to count occurrences and shrink window when distinct characters exceed k, ignoring last seen positions.Step 3: Confirm efficiency
This approach maintains O(n) time and handles reuse correctly.Final Answer:
Option A -> Option AQuick Check:
Frequency map with counts is required for reuse scenarios [OK]
- Assuming last seen indices suffice for reuse
- Thinking brute force is necessary
t can be reused unlimited times (i.e., the substring in s must contain at least one occurrence of each character in t, but counts do not matter). Which modification to the sliding window algorithm correctly solves this variant?Solution
Step 1: Understand problem change
Since counts do not matter, only presence of each character is required, so frequency counts can be replaced by boolean presence tracking.Step 2: Modify sliding window logic
Incrementhavewhen a character fromtappears first time in window, decrement when it leaves. Shrink window when all characters are present.Final Answer:
Option D -> Option DQuick Check:
Tracking presence instead of counts solves the reuse variant efficiently [OK]
- Using original count-based logic
- Assuming brute force is needed
- Sorting breaks substring order
Solution
Step 1: Understand the reuse modification
Allowing elements to be reused means the same element can contribute multiple times as the window slides, potentially overlapping.Step 2: Analyze monotonic deque assumptions
The original deque stores indices assuming each element appears once per position. To handle reuse, counts or frequency tracking is needed to maintain correctness.Step 3: Evaluate other options
Monotonic deque without modification fails; max-heap alone is inefficient; segment tree is overkill for this variant.Final Answer:
Option B -> Option BQuick Check:
Tracking counts allows correct max maintenance with reuse [OK]
- Assuming no changes needed for reuse
- Switching to max-heap without efficiency gain
- Thinking monotonic deque is inapplicable
