Sliding window · Medium
A log scanner looks for short excerpts containing exactly a required multiset of markers. Marker order is irrelevant, but repeated markers count separately.
Return every start index of a fixed-length excerpt that contains exactly the required marker counts.
Input
Call
- Implement solve({text, required}).
Markers
- text is the log marker sequence.
- required defines both the excerpt length and the count of every letter. For AAB, two As and one B are required.
Output
Matches
- Return each start index whose next required.length characters have exactly the required letter counts.
- Order does not matter inside a match. Include overlapping matches.
- Return [] for an empty requirement or when no full window matches.
Constraints
- 0 <= text.length <= 100000; 0 <= required.length <= 10000.
- Characters are A..Z. Inputs are valid; no Unicode normalization is needed.
Examples
Example 1
{
"required": "ABC",
"text": "CBAEBABACD"
}Returns
[
0,
6
]Example 2
{
"required": "AAB",
"text": "AAAB"
}Returns
[
1
]Required complexity
O(n + m) time and O(26) auxiliary space excluding output