leetcode: Minimum Window Substring
2017-10-04 10:46
302 查看
url :
https://leetcode.com/problems/minimum-window-substring/description/描述
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”.
Note:
If there is no such window in S that covers all characters in T, return the empty string “”.
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
解题思路
利用快慢指针,在遍历的过程中,记录下快慢指针之间的最短距离。代码
//利用数组代替map, 感觉节约了不少时间 public String minWindow2(String s, String t) { int counter = 0, left = 0, minLen = Integer.MAX_VALUE; String res = ""; int[] curMap = new int[100]; int[] tMap = new int[100]; for(int i = 0; i < t.length(); i++){ tMap[t.charAt(i)-'0']++; } for(int i = 0; i < s.length(); i++){ if(tMap[s.charAt(i)-'0'] > 0 ){ curMap[s.charAt(i)-'0']++; } if(counter < t.length()){ if(tMap[s.charAt(i) - '0'] > 0 && curMap[s.charAt(i) - '0'] <= tMap[s.charAt(i) - '0']){ counter++; } } if(counter == t.length()){ while(tMap[s.charAt(left)-'0'] == 0 || tMap[s.charAt(left) - '0'] < curMap[s.charAt(left) - '0']){ if(curMap[s.charAt(left)-'0'] > 0){ curMap[s.charAt(left)-'0']--; } left++; } if(minLen > (i - left + 1)){ minLen = i - left + 1; res = s.substring(left, i+1); } } } return res; }
//map 版本,该版本代码的可读性较大,所以必要注释就写在此代码中 public String minWindow(String s, String t) { //声明一些表量: //counter:记录在s中找到匹配t中的个数 //left:窗口的最左端位置 //minLen:最小窗口长度,初始化为整型的最大值 //res:保存当前最优的结果 int counter = 0, left = 0, minLen = Integer.MAX_VALUE; String res = ""; Map<Character,Integer> curMap = new HashMap<>(); Map<Character,Integer> tMap = new HashMap<>(); for(int i = 0; i < t.length(); i++){ //初始化T map if(tMap.containsKey(t.charAt(i))){ tMap.put(t.charAt(i),tMap.get(t.charAt(i))+1); }else{ tMap.put(t.charAt(i),1); } } for(int i = 0; i < s.length(); i++){ if(tMap.containsKey(s.charAt(i))){ //如果S中的元素出现在T中,则将其记录到curMap curMap.put(s.charAt(i),curMap.getOrDefault(s.charAt(i),0)+1); } if(counter < t.length()){ if(tMap.containsKey(s.charAt(i)) &&curMap.get(s.charAt(i))<=tMap.get(s.charAt(i))){ //满足匹配条件则counter加1,此时需要注意,T中可能会出现重复的字符,所以IF 条件如上。 counter++; } } if(counter == t.length()){ //进入该if条件,则是找到了第一个匹配T的substring.如果按照while中的条件去移动left,则S的[left,i]子串永远会匹配T while(!tMap.containsKey(s.charAt(left))|| tMap.get(s.charAt(left)) < curMap.get(s.charAt(left))){ if(curMap.containsKey(s.charAt(left))){ //在移动left之前,此时需要对curMap记录的个数进行减1 curMap.put(s.charAt(left),curMap.get(s.charAt(left))-1); } left++; } if(minLen > (i - left + 1)){ //看看是否需要更新结果 minLen = i - left + 1; res = s.substring(left, i+1); } } } //最后返回结果 return res; }
相关文章推荐
- [leetcode] Minimum Window Substring
- leetcode Minimum Window Substring
- LeetCode Minimum Window Substring
- leetcode--Minimum Window Substring
- [LeetCode] Minimum Window Substring 最小窗口子串
- Leetcode: Minimum Window Substring
- LeetCode - Minimum Window Substring
- LeetCode76 Minimum Window Substring(两种解法)
- [LeetCode P76] Minimum Window Substring
- leetcode 76 Minimum Window Substring/ 209 Minimum Size Subarray Sum
- LeetCode-Minimum Window Substring-最小窗口子串-滑动窗口算法(尺取法)
- Minimum Window Substring -- LeetCode
- LeetCode Minimum Window Substring
- LeetCode Minimum Window Substring
- [LeetCode] Minimum Window Substring
- [Leetcode] Minimum Window Substring
- [LeetCode] Minimum Window Substring
- leetcode Minimum Window Substring答案解析
- LeetCode - Minimum Window Substring
- [LeetCode] Minimum Window Substring