Minimum Window Substring
来源:互联网 发布:淘宝店主真人内衣展示 编辑:程序博客网 时间:2024/06/05 05:44
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 = "ADOBECODEBANC"
T = "ABC"
Minimum window is "BANC"
.
public String minWindow1(String S, String T) { if (S.length()==0||T.length()==0||S.length()<T.length()) return ""; int left=T.length(),start=-1,end=S.length(); Deque<Integer> queue= new LinkedList<Integer>(); Map<Character,Integer> map= new HashMap<Character,Integer>(); for (int i=0;i<T.length();i++){ char c= T.charAt(i); map.put(c,map.containsKey(c)?map.get(c)+1:1); } for (int i =0;i<S.length();i++){ char c= S.charAt(i); if (!map.containsKey(c)) continue; int n = map.get(c); map.put(c,n-1); queue.add(i); if (n>0) left--; char head = S.charAt(queue.peek()); while(map.get(head)<0){ queue.poll(); map.put(head,map.get(head)+1); head=S.charAt(queue.peek()); } if (left==0){ int new_length=queue.peekLast()-queue.peek()+1; if (new_length<end-start) { start=queue.peek(); end=queue.peekLast()+1; } } } if (left==0) return S.substring(start,end); else return ""; }
0 0
- LeetCode: Minimum Window Substring
- LeetCode Minimum Window Substring
- LeetCode : Minimum Window Substring
- [LeetCode] Minimum Window Substring
- [Leetcode] Minimum Window Substring
- Minimum Window Substring
- LeetCode: Minimum Window Substring
- Minimum Window Substring
- [LeetCode]Minimum Window Substring
- Minimum Window Substring
- Minimum Window Substring
- [leetcode]Minimum Window Substring
- Minimum Window Substring
- Leetcode:Minimum Window Substring
- LeetCode-Minimum Window Substring
- [leetcode] Minimum Window Substring
- [leetcode] Minimum Window Substring
- leetcode Minimum Window Substring
- Shortest Palindrome
- Android Api Demos登顶之路(八十五)Graphics-->PurgeableBitmap
- Expression Add Operators
- 注册和登录的测试服务器搭建
- Dungeon Game
- Minimum Window Substring
- Best Time to Buy and Sell Stock IV
- Adaboost 算法的原理与推导
- *LeetCode-Largest Number
- Regular Expression Matching
- QSqlDatabase类
- QSqlQuery类
- Candy
- 写博客想写什么就写点什么