Java 最小窗口子串解决方案的时间复杂度

Java 最小窗口子串解决方案的时间复杂度,java,algorithm,time,Java,Algorithm,Time,最小窗口子串 这是Leetcode的一个问题 我找到了一个基于滑动窗口算法的解决方案,但我无法计算时间复杂度。有些人说是O(N),但我认为不是。请帮帮我,谢谢 public class Solution { // Minimum Window Algorithm, the algorithm must fit for specific problem, this problem is diff from ...words // 348ms public Strin

最小窗口子串

这是Leetcode的一个问题

我找到了一个基于滑动窗口算法的解决方案,但我无法计算时间复杂度。有些人说是O(N),但我认为不是。请帮帮我,谢谢

    public class Solution {
    // Minimum Window Algorithm, the algorithm must fit for specific problem, this problem is diff from ...words
    // 348ms
    public String minWindow(String s, String t) {
        int N = s.length(), M = t.length(), count = 0;
        String res = "";
        if (N < M || M == 0)    return res;
        int[] lib = new int[256], cur = new int[256];  // ASCII has 256 characters
        for (int i = 0; i < M; lib[t.charAt(i++)]++);  // count each characters in t
        for (int l = 0, r = 0; r < N; r++) {
            char c = s.charAt(r);
            if (lib[c] != 0) {
                cur[c]++;
                if (cur[c] <= lib[c])   count++;
                if (count == M) {
                    char tmp = s.charAt(l);
                    while (lib[tmp] == 0 || cur[tmp] > lib[tmp]) {
                        cur[tmp]--;
                        tmp = s.charAt(++l);
                    }
                    if (res.length() == 0 || r - l + 1 < res.length()) 
                        res = s.substring(l, r + 1);
                    count--;  // should add these three lines for the case cur[c] c is char in s but not the one visited
                    cur[s.charAt(l)]--;
                    l++;
                }
            }
        }
        return res;
    }
 }
公共类解决方案{
//最小窗口算法,该算法必须适合特定的问题,这个问题与…字不同
//348ms
公共字符串窗口(字符串s、字符串t){
int N=s.length(),M=t.length(),count=0;
字符串res=“”;
如果(N
有N个步骤可以将s中的每个字符添加到
r
位置

while操作员不超过O(N)
-最多N个工作循环,进行
++l
操作,最多N次无价值的while
检查

因此,如果我们不考虑
s.substring
,那么总体复杂性是线性的


请注意,子字符串操作应该移出循环,我们必须只保留最佳索引对,并在最末端获得子字符串。

有N个步骤可以将s中的每个字符添加到
r
位置

check out my solution:

public class Solution {
    public String minWindow(String S, String T) {
        Map<Character, Integer> pattern = new HashMap<Character, Integer>();
        Map<Character, Integer> cur = new HashMap<Character, Integer>();
        Queue<Integer> queue = new LinkedList<Integer>();
        int min = Integer.MAX_VALUE;
        int begin = 0, end = 0;

        // fill in pattern by T
        for(int i = 0;i < T.length();i++) addToMap(pattern, T.charAt(i));

        // initialize current set
        for(int i = 0;i < T.length();i++) cur.put(T.charAt(i), 0);

        // go through S to match the pattern by minimum length
        for(int i = 0;i < S.length();i++){
            if(pattern.containsKey(S.charAt(i))){
                queue.add(i);
                addToMap(cur, S.charAt(i));
                // check if pattern is matched
                while(isMatch(pattern, cur)){  /* Important Code! */
                    if(i - queue.peek() < min){
                        min = i - queue.peek();
                        begin = queue.peek();
                        end = i+1;
                    }
                    cur.put(S.charAt(queue.peek()), cur.get(S.charAt(queue.peek()))-1);
                    queue.poll();
                }
            }
        }

        return end > begin?S.substring(begin, end):"";
    }

    private void addToMap(Map<Character, Integer> map, Character c){
        if(map.containsKey(c))
            map.put(c, map.get(c)+1);
        else
            map.put(c,1);
    }

    private boolean isMatch(Map<Character, Integer> p, Map<Character, Integer> cur){
        for(Map.Entry<Character, Integer> entry: p.entrySet())
            if(cur.get((char)entry.getKey()) < (int)entry.getValue()) return false;
        return true;
    }
}
while操作员不超过O(N)
-最多N个工作循环,进行
++l
操作,最多N次无价值的while
检查

因此,如果我们不考虑
s.substring
,那么总体复杂性是线性的

请注意,应该将子字符串操作移出循环,我们必须仅保留最佳索引对,并在最末端获取子字符串。

查看我的解决方案:
check out my solution:

public class Solution {
    public String minWindow(String S, String T) {
        Map<Character, Integer> pattern = new HashMap<Character, Integer>();
        Map<Character, Integer> cur = new HashMap<Character, Integer>();
        Queue<Integer> queue = new LinkedList<Integer>();
        int min = Integer.MAX_VALUE;
        int begin = 0, end = 0;

        // fill in pattern by T
        for(int i = 0;i < T.length();i++) addToMap(pattern, T.charAt(i));

        // initialize current set
        for(int i = 0;i < T.length();i++) cur.put(T.charAt(i), 0);

        // go through S to match the pattern by minimum length
        for(int i = 0;i < S.length();i++){
            if(pattern.containsKey(S.charAt(i))){
                queue.add(i);
                addToMap(cur, S.charAt(i));
                // check if pattern is matched
                while(isMatch(pattern, cur)){  /* Important Code! */
                    if(i - queue.peek() < min){
                        min = i - queue.peek();
                        begin = queue.peek();
                        end = i+1;
                    }
                    cur.put(S.charAt(queue.peek()), cur.get(S.charAt(queue.peek()))-1);
                    queue.poll();
                }
            }
        }

        return end > begin?S.substring(begin, end):"";
    }

    private void addToMap(Map<Character, Integer> map, Character c){
        if(map.containsKey(c))
            map.put(c, map.get(c)+1);
        else
            map.put(c,1);
    }

    private boolean isMatch(Map<Character, Integer> p, Map<Character, Integer> cur){
        for(Map.Entry<Character, Integer> entry: p.entrySet())
            if(cur.get((char)entry.getKey()) < (int)entry.getValue()) return false;
        return true;
    }
}
公共类解决方案{ 公共字符串窗口(字符串S、字符串T){ 映射模式=新的HashMap(); Map cur=new HashMap(); Queue Queue=new LinkedList(); int min=整数最大值; int begin=0,end=0; //用T填充图案 对于(inti=0;i开始?S.子字符串(开始,结束):“”; } 私有void addToMap(映射映射,字符c){ if(图c) map.put(c,map.get(c)+1); 其他的 图.put(c,1); } 私有布尔isMatch(映射p,映射cur){ 对于(Map.Entry:p.entrySet()) if(cur.get((char)entry.getKey())<(int)entry.getValue())返回false; 返回true; } }
查看我的解决方案:
公共类解决方案{
公共字符串窗口(字符串S、字符串T){
映射模式=新的HashMap();
Map cur=new HashMap();
Queue Queue=new LinkedList();
int min=整数最大值;
int begin=0,end=0;
//用T填充图案
对于(inti=0;i开始?S.子字符串(开始,结束):“”;
}
私有void addToMap(映射映射,字符c){
if(图c)
map.put(c,map.get(c)+1);
其他的
图.put(c,1);
}
私有布尔isMatch(映射p,映射cur){
对于(Map.Entry:p.entrySet())
if(cur.get((char)entry.getKey())<(int)entry.getValue())返回false;
返回true;
}
}

一般来说,更长的更具描述性的变量名有助于代码的可读性谢谢你的建议。一般来说,更长的更具描述性的变量名有助于代码的可读性谢谢你的建议