Idea
- Use hashmap to keep track the occurrence of each character in the desired string
- Use sliding window to find the the minimum window sub-string
- Keep expanding window when substring isn’t found
- Keep shrinking window to find minimal when the desired string is found in the substring window
- RMB ❗❗❗
- HashMap returns Integer object, so we need to use equals() to compare using values instead of object address
Space & time analysis
- Space O(1), because there are only 26 characters
- Time O(n), in worst case, we need to process each character twice (add to window & remove from window)
Code
- 1st attempt (java)
- 2nd attempt (java) ❤️
- 3rd attempt (java) ❤️❤️ (cleaner, but less space efficient than 2nd attempt)