您的位置:首页 > 编程语言 > Java开发

leetcode:Minimum Window Substring题解(Java)

2016-07-08 20:49 429 查看
原题:

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. 

题意是要我们在给定的S字符串中找到包含T中所有字符的最小子串,要求复杂度是O(n)

暴力解法:从下标为0开始遍历S,设下标为i,设变量j从当前位置i遍历,一直到包含T的所有字符位置,比较i,j包含的长度,并保存最小的长度。提交结果:Time
Limit Exceeded,
时间复杂度是O(n^2),不合题意

代码如下:

public String minWindow(String s, String t) {
if(s.length()<t.length() || t=="") return "";

        String tempStr=null;

        String str=s;

        List<Character> charls = new ArrayList<Character>();

        

        for (int i = 0; i < s.length()-t.length()+1; i++) {

             charls.clear();

            for (int k = 0; k < t.length(); k++) {

      charls.add(t.charAt(k));

      }

            int j;

        for ( j = i; j < s.length(); j++) {

        if(charls.contains(s.charAt(j))){

        charls.remove(charls.indexOf(s.charAt(j)));

        }
if(charls.size()==0 || j==s.length()-1) break;
}

        if(charls.size()==0){

        tempStr=s.substring(i, j+1);

        str=str.length()<tempStr.length()?str:tempStr;

        }

        if(i==0 && j==s.length()-1 && charls.size()>0) return "";
}
return str;

    }

符合题意的解法:时间复杂度为O(n)

public String minWindow(String s, String t) {
   if(t.length()>s.length()) 
       return "";
   String result = "";

   HashMap<Character, Integer> target = new HashMap<Character, Integer>();
   for(int i=0; i<t.length(); i++){
       char c = t.charAt(i);    
       if(target.containsKey(c)){
           target.put(c,target.get(c)+1);
       }else{
           target.put(c,1);  
       }
   }

    HashMap<Character, Integer> map = new HashMap<Character, Integer>();
   int left = 0;
   int minLen = s.length()+1;
   int count = 0; 
    
   for(int i=0; i<s.length(); i++){
       char c = s.charAt(i);
       if(target.containsKey(c)){
           if(map.containsKey(c)){
               if(map.get(c)<target.get(c)){
                   count++;
               }
               map.put(c,map.get(c)+1);
           }else{
               map.put(c,1);
               count++;
          }
       }

       if(count == t.length()){
           char sc = s.charAt(left);
           while (!map.containsKey(sc) || map.get(sc) > target.get(sc)) {
               if (map.containsKey(sc) && map.get(sc) > target.get(sc))
                map.put(sc, map.get(sc) - 1);
               left++;
               sc = s.charAt(left);
           }
           
           if (i - left + 1 < minLen) {
               result = s.substring(left, i + 1);
               minLen = i - left + 1;
           }
       }
   }

   return result;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: