| Problem Names | Solution Writeups | Code |
|---|---|---|
| Best Time to Buy & Sell Stock |
Create two pointers, point at index 0 and 1. Keep updating second pointer, and when encountering prices[first] > prices[second], update first to second, and second++. |
|
| Longest Non-repeated Substring |
Create left and right pointer and a set to check duplicates. Check if s[right] exists in set, continuously remove s[left++] until s[right] not in set. Record largest length as well. |
|
| Longest Repeating Character Replacement |
Create left and right pointer, continuously add s[right] to a hash table, updates right per iteration, updates left when current substring length - highest frequency character > k. |
|
| Permutation in String |
The trick is that it takes O(1) to check if one string is a permutation of another string by simply count the frequency of each character. If there exists substring with matches == 26, then it contains a permutation of another string. |
|
| Minimum Window Substring |
Maintain a window by having left and right pointers, continuously check if that window of string satisfies the character count of another string. We can speed up this checking by having two variables have and need, so if have == need, it is a potential answer. Once a possible answer is found, update left pointer from the window by keep +1 until have != need. Else, keep incrementing right by 1. Only record the minimum subtring found so far. |
|
| Sliding Window Maximum |
The core idea to solve this question is by using the deque data structure which supports popping first and last element. In the deque, we only need to keep the max of the next window, and remove redundant numbers that are definitely not max. This can be assured by keep popping back if curr_val > deque[last]. Pop front if left pointer value > deque[front]. Hence, instead of storing the value inside deque, store the index of that value. |