Problem
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
For example,
S =
T =
S =
"ADOBECODEBANC"T =
"ABC"
Minimum window is
"BANC".
from https://leetcode.com/problems/minimum-window-substring/
Solution:There is a very nice post on this problem. Here is the link: http://n00tc0d3r.blogspot.com/2013/04/minimum-window-substring.html
Issue:
One the issue I found when I coded this problem is that I used LinkedList to store the next index of the needed characters. And the system throws an Time Limit Exceeded exception. Then, I start to carefully compare my code with code of the above link. I found the only difference is that that post used ArrayList instead of LinkedList.
Here is the difference between ArrayList and LinkedList
1. LinkedList implements it with a doubly-linked list. ArrayList implements it with a dynamically resizing array
2.
LinkedList<E> allows for constant-time insertions or removals using iterators, but only sequential access of elements; ArrayList<E>, on the other hand, allow fast random read access, so you can grab any element in constant time.3. Also, if you have large lists, keep in mind that memory usage is also different. Each element of a LinkedList has more overhead since pointers to the next and previous elements are also stored. ArrayLists don't have this overhead
Therefore, when you are not sure, start with ArrayList
没有评论:
发表评论