Topics
Given an array, find the longest subarray where the sum is less than or equal to k
- Use the sliding window approach with two pointers
left = right = 0
- Adjust the window by expanding the
right
pointer and shrinking theleft
pointer when the sum exceedsk
while current_sum > k: current_sum -= arr[left++]
- Time: vs. brute-force (checking all subarrays).
Find the length of the longest substring without repeating characters.
left
andright
pointers define a sliding window.- A hash map (
char_map
) stores the most recent index of each character in the current window.- While
right
is within string bounds:
- If
s[right]
is inchar_map
: Moveleft
tomax(left, char_map[s[right]] + 1)
(moveleft
past the previous occurrence)- Update most recent index for curr char:
char_map[s[right]] = right
- Update
max_length = max(max_length, right - left + 1)
- Increment
right
- Time: vs. brute-force (checking all substrings).
Given two strings s1 and s2, return true if s2 contains a permutation of s1
- Use a sliding window of the size of
s1
overs2
. Use frequency arrays to check if the current window is a permutation ofs1
Given two strings s and t, find the minimum window in s which will contain all the characters in t
- Use two pointers (
left
andright
) to define a window ins
- Use a hash map to store the character counts of
t
- Expand the right pointer until the window contains all characters of
t
(checking against the hash map)- Shrink the
left
pointer as much as possible while still maintaining the condition that the window contains all characters oft
. This is where we minimize the window size