Minimum Window Substring [Hard] — Sliding Window with Frequency Map
Find the minimum window in s that contains all characters of t using a shrinkable sliding window with a character frequency counter.
webcoderspeed.com
61 articles
Find the minimum window in s that contains all characters of t using a shrinkable sliding window with a character frequency counter.
Find the maximum number of points on the same line using a slope-as-fraction HashMap for each anchor point.
Count subarrays with exactly K distinct integers using the formula: atMost(K) - atMost(K-1).
Design a stack that pops the most frequent element (breaking ties by recency) using frequency and group-stacks mapping.
Master HashMap, HashSet, and cache design patterns with 45 problems, 5-language solutions, and MAANG company tags.
Find two indices that add to target in O(n) by storing each number in a HashMap and looking up the complement.
Check if two strings are anagrams by comparing their character frequency counts using an array or HashMap.
Check if a ransom note can be constructed from magazine letters using a character frequency map.
Check if two strings are isomorphic by maintaining bidirectional character mappings to ensure a consistent one-to-one correspondence.
Check if a string follows a given pattern using bidirectional mapping between pattern chars and words.
Determine if a number is happy by repeatedly summing digit squares and detecting cycles using a HashSet or Floyd algorithm.
Find if any two duplicate elements are within k indices of each other using a HashMap of last-seen positions.
Find characters common to all strings in a list by intersecting their frequency arrays with element-wise minimum.
Count how many stones are jewels by storing jewel types in a HashSet and checking each stone.
Find all groups of duplicate files by parsing path strings and grouping files by their content using a HashMap.
Group strings that are anagrams together by using their sorted form as a HashMap key.
Find the k most frequent elements in O(n) using bucket sort by frequency instead of a heap.
Design a Least Recently Used cache with O(1) get and put operations using a HashMap combined with a doubly linked list.
Count subarrays with sum exactly k using prefix sums stored in a HashMap to find valid starting positions in O(n).
Check if a continuous subarray of length >= 2 sums to a multiple of k using prefix sum modulo k with a HashMap.
Find the longest consecutive integer sequence in O(n) by using a HashSet and only starting sequences from their minimum element.
Design a set with O(1) insert, delete, and getRandom using a dynamic array combined with a HashMap for index tracking.
Find all starting indices of anagram substrings by using a fixed window with character frequency comparison.
Implement weighted random selection by building a prefix sum array and using binary search to find the sampled index.
Find the vertical line that crosses the fewest bricks by counting the most frequent gap position using a HashMap.
Check if all value frequencies are unique using two hash sets in O(n).
Count substrings with at most one odd-frequency letter using bitmask prefix XOR and a hash map.
Count (row, col) pairs that are equal by hashing each row tuple and each column tuple.
Find if a BST contains two nodes summing to target using a hash set during DFS traversal.
Find the longest arithmetic subsequence using dp[i][diff] = length ending at index i with given difference.
Count pairs (i,j) where j-i != nums[j]-nums[i] by counting good pairs via frequency map of nums[i]-i.
Find the smallest subarray to remove so the remaining sum is divisible by p using prefix modular arithmetic.
Count tuples (a,b,c,d) where a*b=c*d by counting pairs with each product using a frequency map.
Design a URL shortener using two hash maps for O(1) encode and decode operations.
Convert a fraction to decimal string with recurring part detected via remainder position tracking.
Find all duplicate subtrees by serializing each subtree and tracking serialization counts in a hash map.
Implement a hash map from scratch using an array of buckets with chaining for collision resolution.
Implement a hash set from scratch supporting add, remove, and contains in O(1) average time.
Pick a random index of a target value with equal probability using reservoir sampling over the array.
Find the most common word not in a banned list by normalizing input and using a frequency counter.
Determine if cards can be rearranged into consecutive groups of size groupSize using a sorted frequency map.
Count subarrays with sum divisible by k using prefix mod frequencies and the complement map pattern.
Count pairs summing to a power of 2 by checking all 22 powers-of-2 against a running frequency map.
Design a time-stamped key-value store supporting set and get(key, timestamp) using binary search on sorted timestamps.
Count 4-tuples summing to zero by splitting into two pairs and using a frequency map of pair sums.
Implement an LFU cache with O(1) get/put using three hash maps: key→val, key→freq, freq→OrderedDict.
Design a data structure supporting inc/dec and getMaxKey/getMinKey in O(1) using a doubly linked list of frequency buckets.
Design Twitter with follow/unfollow and getNewsFeed using per-user tweet lists and a min-heap merge.
Find all index pairs forming palindromes by checking prefix/suffix splits against a reverse-word map.
Complete cheatsheet for Hashing & Maps: all 7 patterns, Big O reference, template code, and MAANG priority guide.
Deep copy a linked list with random pointers using either a hash map or the O(1) space interleave technique.
Remove consecutive nodes that sum to zero by tracking prefix sums in a map and relinking past zero-sum runs.
Find the next greater element for each query in O(n+m) using a monotonic stack on nums2 and a lookup map.
Count paths in a binary tree summing to target (not necessarily root-to-leaf) using a prefix sum frequency map.
Find the maximum fruits you can collect starting from any tree with two baskets (at most 2 distinct fruit types) using a sliding window.
Count tuples (i,j,k,l) where nums1[i]+nums2[j]+nums3[k]+nums4[l]=0 by hashing all pairs from first two arrays.
Count subarrays with exactly K distinct integers using the mathematical trick: exactly(K) = atMost(K) - atMost(K-1).
Count subarrays with sum divisible by k using prefix sum modulo k and a frequency map of remainders.
Find all starting indices of substrings that are concatenations of all given words using a sliding window for each possible word-aligned start.
Find the minimum number of consecutive cards that contain a matching pair using a HashMap tracking last seen positions.
Find the longest substring containing at most 2 distinct characters using a variable sliding window with a character count map.