Sliding Window
25 / 170Sliding WindowLesson 4 of 6About 53 min
When the width never changes, the window becomes a rolling comparison against a target.
The problem asks whether any window of a fixed width matches a target exactly, where exactly means the same characters with the same counts. You know how to roll a fixed window, you know how to build a count map, and you know from Unit 1 that two count maps compare equal when their contents agree. Put those together.
averages_of_width returns the average of every window of the given width, rolling the total rather than resumming. first_window_with_all_distinct returns the start index of the first window of the given width whose characters are all distinct, or -1. matches_target_counts reports whether a window of text starting at start has exactly the character counts given in target.
Tests
print(averages_of_width([1, 2, 3, 4], 2))print(first_window_with_all_distinct("abcabc", 3), first_window_with_all_distinct("aaaa", 2))print(matches_target_counts("abcb", 1, {"b": 2, "c": 1}), matches_target_counts("abcb", 0, {"b": 2, "c": 1}))Output
Run tests to see output.
Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.