15% off all plans & roadmaps Use code
See plansLeetCode 567 · Solution & editorial
Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise.
Permutation in String is a Sliding Window problem, in the Character Frequency Matching subpattern. The pattern covers fixed-size, variable-size, and character frequency matching window techniques
Learn sliding window with character frequency matching for anagram and permutation substring problems.
The reference solution runs in O(m) time and O(1) space.
Permutation in String has been reported in interviews at Adobe, Amazon, Bloomberg, Facebook, Google, Microsoft, Yandex.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Permutation in String is a Sliding Window problem, in the Character Frequency Matching subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.