leetcode: Minimum Window Substring

来源:互联网 发布:蓝月传奇龙魂数据 编辑:程序博客网 时间:2024/06/17 02:36

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;    }
原创粉丝点击