MEDIUM NC#18 Sliding Window
567. Permutation in String
๐ Problem
Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise. In other words, return true if one of s1's permutations is the substring of s2.
๐ง Visual Learning Aid
1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases
JS/TS Refreshers
- โขArray methods (`push`, `pop`, `shift`, `slice`)
- โขObject/Map/Set usage patterns
- โขFunction parameter and return typing
- โขString/array indexing
- โขSet/Map frequency counting
- โขPointer movement invariants
Logical Thinking Concepts
- โขDefine invariants before coding
- โขCheck edge cases first (`[]`, single element, duplicates)
- โขEstimate time/space before implementation
- โขApply Sliding Window reasoning pattern
๐ก Approach
- โ Permutation means same characters with same counts
- โ Use sliding window of size s1.length on s2
- โ Track character counts in current window
- โ Expand right and shrink left while maintaining window size
- โ Optimize by tracking count of matching characters
- โ Time: O(len(s1) + len(s2)), Space: O(26) constant
๐งญ Prerequisites
๐ ๏ธ Hints & Pitfalls
Hints
- โขPermutation means same characters with same counts
- โขUse sliding window of size s1.length on s2
- โขTrack character counts in current window
Common Pitfalls
- โขExpand right and shrink left while maintaining window size
- โขOptimize by tracking count of matching characters
- โขTime: O(len(s1) + len(s2)), Space: O(26) constant
๐งช Test Cases
Hidden tests on submit: 2
Test Case 1
Not run Input:
checkInclusion('ab', 'eidbaooo'); Expected:
true Test Case 2
Not run Input:
checkInclusion('ab', 'eidboaoo'); Expected:
false Test Case 3
Not run Input:
checkInclusion('adc', 'dcda'); Expected:
true ๐ Code Editor
๐ค Output