Permutation in String
Permutation in String is LeetCode 567 (Medium). Given two strings s1 and s2, return true if some rearrangement of s1 appears in s2 as a block of consecutive letters, and false otherwise.
- A rearrangement keeps every letter of
s1the same number of times, so only letter counts matter, not order. - Both strings use lowercase letters only and can be 10⁴ letters long.
- If
s1is longer thans2, no block ofs2is long enough, and the answer is false.
- 1 <= s1.length, s2.length <= 10⁴
- s1 and s2 consist of lowercase English letters.
Intuition
Only blocks of s2 exactly as long as s1 can be rearrangements of it, and two neighbouring blocks differ by one letter at each end. So slide a window of length len(s1) across s2.
Keep need[c] = (count of c in s1) − (count of c in the window). Positive means the window is short of c; negative means it has extra. Instead of checking all 26 letters at every step, keep one number, off, the total of how far each letter is from zero. A letter entering or leaving changes one entry of need by one, so off moves by exactly one, and the window is a rearrangement of s1 exactly when off is 0.
"Does any block of length k have the same letters as a pattern?" is a fixed-length window over letter counts. When you only need a yes/no, one running mismatch number replaces comparing whole count arrays.
Approach
Before reading on, take a window that is short one a. Decide how one number should change when an a enters, and when a b the window already had too many of leaves. Aim for O(n) time with O(1) work per step.
Count what the window owes
Fill need with the letter counts of s1 and set off = len(s1). An empty window is short of every letter of s1, so the total distance from a match is the length of s1.
Let a letter enter
For each new letter c of s2: if need[c] > 0 the window was short of c, so off drops by 1; otherwise c is an extra, so off grows by 1. Then decrease need[c].
Let a letter leave
Once the window would be longer than s1, remove the letter len(s1) places back: increase need for it, then add 1 to off if that letter is now owed, or subtract 1 if it was an extra. The window stays exactly len(s1) letters long.
Check the counter
After both moves, off == 0 means every letter count matches s1, so return true right away. If the window reaches the end of s2 without that happening, return false.
Code in Python, C++ and Java
Common pitfalls
Updating the leaving letter in the wrong order
off += 1 if need[old] > 0 else -1 need[old] += 1
need[old] += 1 off += 1 if need[old] > 0 else -1
The test must look at the count after the letter has gone. For s1 = "ab", s2 = "bxa" the b leaves while its count is exactly right. Tested too early, it lowers off to 0 instead of raising it, and the code returns true for the window "xa".
Edge cases
The window never holds all three letters, so off stays above 0 and the loop ends with false. No separate length check is needed.
Complexity
s1 takes one pass, and each letter of s2 enters once and leaves once, each move costing a constant number of steps. The counts cover at most 26 letters, whatever the string lengths.Permutation in String FAQ
How is this different from Find All Anagrams in a String?
It is the same window, but Find All Anagrams in a String needs every starting index, while this problem needs only a yes/no and can stop at the first match. That page compares two count arrays; the mismatch counter here is a way to make each check a single comparison.
Why not generate every permutation of s1 and search for each?
s1 can have 10⁴ letters, and even 10 distinct letters already give 3,628,800 orders. Counting letters makes the order irrelevant, which is exactly what "any permutation" means.
Does it work for uppercase or Unicode text?
In Python, Counter works for any characters unchanged. In C++ and Java, swap the 26-slot array for a hash map keyed by character; the counter logic stays the same.