Keep a frequency map inside the window to compare stretches with a pattern.
The counts map holds exactly the plays from left to right, and matched equals the number of mix tracks whose window count equals their count in the mix.
Use it when a window must be compared with a multiset, or the question asks how many of each value, or how many different values, a window holds. Signal words: in any order, same counts, a rearrangement, an anagram, how many different, a window of k.
State: a counts map for the window. To compare with a pattern, add a need map for the pattern and a matched counter.
Slide: the count of the value at right - k goes down by one; the count of the value at right goes up by one.
Matched: change it only when a needed count reaches its need or moves away from it. The window matches when matched equals the number of distinct values in the pattern.
Distinct values: delete a key when its count reaches 0. The size of the map is then the number of distinct values in the window.
Cost: each value joins once and leaves once, with O(1) average work per hash map update, so the whole pass is O(n + m).
def find_reorderings(values, pattern):
k = len(pattern)
starts = []
if k > len(values):
return starts
# [1] Count what the pattern needs.
need = {}
for value in pattern:
need[value] = need.get(value, 0) + 1
window = {}
matched = 0
for right in range(len(values)):
# [2] Once the window is full, the value at right - k leaves.
if right >= k:
leaving = values[right - k]
count = window[leaving]
if leaving in need:
if count == need[leaving]:
matched -= 1 # its count is about to drop below the need
elif count == need[leaving] + 1:
matched += 1 # its count is about to come back to the need
window[leaving] = count - 1
# [3] The value at right joins: update its count, then matched.
joining = values[right]
count = window.get(joining, 0) + 1
window[joining] = count
if joining in need:
if count == need[joining]:
matched += 1 # it has reached the need
elif count == need[joining] + 1:
matched -= 1 # it has passed the need
# [4] Every needed value has exactly its count: record the start.
if matched == len(need):
starts.append(right - k + 1)
return startspublic int[] findReorderings(int[] values, int[] pattern) {
int k = pattern.length;
List<Integer> starts = new ArrayList<>();
if (k > values.length) {
return new int[0];
}
// [1] Count what the pattern needs.
Map<Integer, Integer> need = new HashMap<>();
for (int value : pattern) {
need.put(value, need.getOrDefault(value, 0) + 1);
}
Map<Integer, Integer> window = new HashMap<>();
int matched = 0;
for (int right = 0; right < values.length; right++) {
// [2] Once the window is full, the value at right - k leaves.
if (right >= k) {
int leaving = values[right - k];
int count = window.get(leaving);
if (need.containsKey(leaving)) {
if (count == need.get(leaving)) {
matched--; // its count is about to drop below the need
} else if (count == need.get(leaving) + 1) {
matched++; // its count is about to come back to the need
}
}
window.put(leaving, count - 1);
}
// [3] The value at right joins: update its count, then matched.
int joining = values[right];
int count = window.getOrDefault(joining, 0) + 1;
window.put(joining, count);
if (need.containsKey(joining)) {
if (count == need.get(joining)) {
matched++; // it has reached the need
} else if (count == need.get(joining) + 1) {
matched--; // it has passed the need
}
}
// [4] Every needed value has exactly its count: record the start.
if (matched == need.size()) {
starts.add(right - k + 1);
}
}
int[] result = new int[starts.size()];
for (int i = 0; i < result.length; i++) {
result[i] = starts.get(i);
}
return result;
}vector<int> findReorderings(const vector<int>& values, const vector<int>& pattern) {
int k = pattern.size();
vector<int> starts;
if (k > (int) values.size()) {
return starts;
}
// [1] Count what the pattern needs.
unordered_map<int, int> need;
for (int value : pattern) {
need[value]++;
}
unordered_map<int, int> window;
int matched = 0;
for (int right = 0; right < (int) values.size(); right++) {
// [2] Once the window is full, the value at right - k leaves.
if (right >= k) {
int leaving = values[right - k];
int count = window[leaving];
if (need.count(leaving)) {
if (count == need[leaving]) {
matched--; // its count is about to drop below the need
} else if (count == need[leaving] + 1) {
matched++; // its count is about to come back to the need
}
}
window[leaving] = count - 1;
}
// [3] The value at right joins: update its count, then matched.
int joining = values[right];
int count = window[joining] + 1;
window[joining] = count;
if (need.count(joining)) {
if (count == need[joining]) {
matched++; // it has reached the need
} else if (count == need[joining] + 1) {
matched--; // it has passed the need
}
}
// [4] Every needed value has exactly its count: record the start.
if (matched == (int) need.size()) {
starts.push_back(right - k + 1);
}
}
return starts;
}function findReorderings(values, pattern) {
const k = pattern.length;
const starts = [];
if (k > values.length) {
return starts;
}
// [1] Count what the pattern needs.
const need = new Map();
for (const value of pattern) {
need.set(value, (need.get(value) || 0) + 1);
}
const window = new Map();
let matched = 0;
for (let right = 0; right < values.length; right++) {
// [2] Once the window is full, the value at right - k leaves.
if (right >= k) {
const leaving = values[right - k];
const count = window.get(leaving);
if (need.has(leaving)) {
if (count === need.get(leaving)) {
matched--; // its count is about to drop below the need
} else if (count === need.get(leaving) + 1) {
matched++; // its count is about to come back to the need
}
}
window.set(leaving, count - 1);
}
// [3] The value at right joins: update its count, then matched.
const joining = values[right];
const count = (window.get(joining) || 0) + 1;
window.set(joining, count);
if (need.has(joining)) {
if (count === need.get(joining)) {
matched++; // it has reached the need
} else if (count === need.get(joining) + 1) {
matched--; // it has passed the need
}
}
// [4] Every needed value has exactly its count: record the start.
if (matched === need.size) {
starts.push(right - k + 1);
}
}
return starts;
}def distinct_counts(values, k):
result = []
window = {}
for right in range(len(values)):
# [1] Once the window is full, the value at right - k leaves.
if right >= k:
leaving = values[right - k]
window[leaving] -= 1
if window[leaving] == 0:
del window[leaving] # a value with no plays left is not in the window
# [2] The value at right joins the window.
joining = values[right]
window[joining] = window.get(joining, 0) + 1
# [3] A full window: the number of keys is the number of distinct values.
if right >= k - 1:
result.append(len(window))
return resultpublic int[] distinctCounts(int[] values, int k) {
List<Integer> result = new ArrayList<>();
Map<Integer, Integer> window = new HashMap<>();
for (int right = 0; right < values.length; right++) {
// [1] Once the window is full, the value at right - k leaves.
if (right >= k) {
int leaving = values[right - k];
window.put(leaving, window.get(leaving) - 1);
if (window.get(leaving) == 0) {
window.remove(leaving); // a value with no plays left is not in the window
}
}
// [2] The value at right joins the window.
int joining = values[right];
window.put(joining, window.getOrDefault(joining, 0) + 1);
// [3] A full window: the number of keys is the number of distinct values.
if (right >= k - 1) {
result.add(window.size());
}
}
int[] out = new int[result.size()];
for (int i = 0; i < out.length; i++) {
out[i] = result.get(i);
}
return out;
}vector<int> distinctCounts(const vector<int>& values, int k) {
vector<int> result;
unordered_map<int, int> window;
for (int right = 0; right < (int) values.size(); right++) {
// [1] Once the window is full, the value at right - k leaves.
if (right >= k) {
int leaving = values[right - k];
window[leaving]--;
if (window[leaving] == 0) {
window.erase(leaving); // a value with no plays left is not in the window
}
}
// [2] The value at right joins the window.
int joining = values[right];
window[joining]++;
// [3] A full window: the number of keys is the number of distinct values.
if (right >= k - 1) {
result.push_back((int) window.size());
}
}
return result;
}function distinctCounts(values, k) {
const result = [];
const window = new Map();
for (let right = 0; right < values.length; right++) {
// [1] Once the window is full, the value at right - k leaves.
if (right >= k) {
const leaving = values[right - k];
window.set(leaving, window.get(leaving) - 1);
if (window.get(leaving) === 0) {
window.delete(leaving); // a value with no plays left is not in the window
}
}
// [2] The value at right joins the window.
const joining = values[right];
window.set(joining, (window.get(joining) || 0) + 1);
// [3] A full window: the number of keys is the number of distinct values.
if (right >= k - 1) {
result.push(window.size);
}
}
return result;
}if set(plays[start:start + k]) == set(mix): # bug: sets forget repeats
starts.append(start)if window_sum == mix_sum: # bug: different windows can share a sum
starts.append(right - k + 1)if joining in need:
matched += 1 # bug: counts plays of mix tracks, not tracks at their needed countwindow[leaving] -= 1 # bug: the key stays even when its count is 0Your team lead brings a different question from the rights team: did a channel play the mix's tracks in the mix's order, even with other tracks in between? For the history [7, 5, 2, 9, 7] and the mix [7, 2, 7], the answer is yes: positions 0, 2 and 4.
systemdesignmastery.dev/dsa/cheatsheets/window-counts