首页 > 图灵资讯 > 技术篇>正文

最小窗口子串

2024-09-18 15:23:33

最小窗口子串

问题

暴力破解方法: tc:o(n^2),sc:o(256)这是常数 注:这将导致 tle

class solution {
    public string minwindow(string s, string t) {


        int min = integer.max_value;
        int start =-1;
        int end = -1;
        //generating all possible substrings and returning the smallest substring having
        //all the character of string t
        for(int i =0;i<s.length int arr new all the asci characters with count of character in t for k="0;k&lt;t.length();k++){" j="i;j&lt;s.length();j++){" if at s is also present increase>0){
                    count++;
                }
                arr[s.charat(j)]--;//decrease the frequency of s.charat(j) by 1 in arr
                if(count ==t.length(){/////) if count == length of t we have found one potential substring 
                    if(min&gt; j-i+1){
                        min  = j-i +1;
                        start  = i;
                        end = j;
                    }
                    break;// break out as there is no need to propagate further in the current iteration
                }
            }
        }
        if(start ==-1 || end ==-1) return "";// if not found 
        return s.substring(start,end+1);// else
    }
}
</s.length>

最佳方法: tc: o(n) , sc:o(256) 是常数

// this intuition will require solving such problems as much as possible
class Solution {
    public String minWindow(String s, String t) {
        int left=0,right=0;
        int min = Integer.MAX_VALUE;
        int count =0;
        int arr[] = new int[256]; // has table for keeping count of characters
        // keeping track of start and end index
        int start =-1;
        int end = -1;
        for(int i=0;i<t.length arr while char c="s.charAt(right);" if>0){ // if the character is part of t, then increment count
                count++;
            }
            arr[c]--;// decrease the count of the character in the hash table once it is taken into consideration 
            while(count ==t.length()){ // if count == t.size() we have found a potential window
                if(min &gt; right-left+1){
                    min = right-left+1;
                    //update the start and end accordingly 
                    start = left;
                    end = right;
                }
                arr[s.charAt(left)]++;//since we are removing this character from window and increasing the left pointer to point to next character after this, there will be one less
                //character that is not part of the t 
                if(arr[s.charAt(left)]&gt;0){ // if after doing +1 for arr[s.charAt(left)] character, if it become positive then it will mean that this was part of t, and now we will have to look for this character in the next window 
                    count--; // since this was part of t, then count should decrement by 1
                }
                left++;
            }
            right++;
        }
        if(start ==-1 || end ==-1) return "";
        return s.substring(start,end+1);
    }
}
</t.length>

以上是最小窗口串的详细内容。请关注图灵教育的其他相关文章!

上一篇 函数式编程与 OOP 编程在设计模式中的体现
下一篇 返回列表

文章素材均来源于网络,如有侵权,请联系管理员删除。