Permutation in String
Problem
You are given two strings, a short pattern and a longer text. Determine whether the text contains a contiguous run of characters that is some rearrangement of the pattern: a permutation of the exact same letters, in any order.
Example. For pattern "ab" and text "eidbaooo", the substring "ba" is a rearrangement of "ab", so the answer is true.
Key idea
Generating every permutation of the pattern and searching for each one is combinatorially expensive, and separately checking every substring of the text for a full anagram match from scratch costs a lot of repeated counting. Because a match must be exactly as long as the pattern, use a fixed-size sliding window equal to the pattern's length, and slide it one character at a time across the text. Maintain a running count of each letter currently inside the window alongside the pattern's own letter counts. As the window slides, adding the new rightmost character and removing the departing leftmost character keeps the running counts current in constant time, rather than recounting the whole window. The window is a match exactly when its counts equal the pattern's counts, which can be checked directly or tracked incrementally with a small "how many letters currently match" counter.
Solution
Complexity
- Time: O(n). The window slides once across the text, with constant work per step since there are only 26 letters.
- Space: O(1). Two fixed 26-entry count arrays, regardless of input length.
Watch out for
- The window must stay exactly the pattern's length; every step both adds one character and removes one, rather than only expanding.
- If the pattern is longer than the text, no match is possible and the answer is immediately false.
- Comparing full count arrays at every step still runs in O(n) time since 26 is a constant, but an incremental match counter avoids that repeated work.
Pattern
This is the fixed-size sliding window with a frequency-equality check, distinct from the variable-size windows used elsewhere. The same window-of-exact-length idea powers "find all anagrams in a string" and other exact-length substring-matching problems.