Sliding window
Episode 21 Permutation in String
▶ This video is coming to YouTube soon.
LeetCode 567, Permutation in String, solved on screen. Two examples show what counts: the same letters as s1, side by side, in any order. Then the brute force counts every window of s2 from scratch and compares it with s1's counts: O(n · m). Finally, one fixed-size sliding window adds the letter coming in and removes the one going out, so each step costs O(1): O(n) in all.
What you'll learn
- What Permutation in String asks, through two worked examples
- Why equal letter counts mean a permutation
- The brute force: count every window, O(n · m) time, O(1) space
- The fixed sliding window: one in, one out, O(n) time, O(1) space
- Why the letter at right − m is the one that leaves
Chapters
- 0:00The problem
- 0:16Examples
- 0:39Brute force, visualized
- 1:21Brute force: the code
- 1:38Brute force, line by line
- 2:19Sliding window: the idea
- 2:38Sliding window, visualized
- 3:22Sliding window: the code
- 3:39Sliding window, line by line
- 4:17Comparing the approaches