zoukankan      html  css  js  c++  java
  • [LeetCode] Minimum Window Substring

    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 emtpy string "".

    If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

    Hide Tags
     Hash Table Two Pointers String
     

    思路:

    双指针,动态维护一个区间。尾指针不断往后扫,当扫到有一个窗口包含了所有 T 的字符后,
    然后再收缩头指针,直到不能再收缩为止。最后记录所有可能的情况中窗口最小的

    时间复杂度 O(n),空间复杂度 O(1)

    class Solution {
        public:
            string minWindow(string S, string T)
            {
                if(S.empty() || T.empty() || S.size() < T.size())
                    return string();
    
                vector<int> expect(256, 0);
                vector<int> appear(256, 0);
    
                for(int i = 0; i < T.size(); i++)
                {   
                    expect[T[i]] ++; 
                }   
    
                int minWidth = INT_MAX, min_start = 0; //
                int win_start = 0;
                int appearCharCnt = 0;
                for(int win_end = 0; win_end < S.size(); win_end++)
                {   
                    if(expect[S[win_end]] > 0) // this char is part of T
                    {   
                        appear[S[win_end]]++;
                        if(appear[S[win_end]] <= expect[S[win_end]])
                            appearCharCnt ++; 
                    }   
                    //cout << "appearCharCnt	" <<appearCharCnt<< endl;
                    if(appearCharCnt == T.size())
                    {   
                        // shrink the start
                        while (appear[S[win_start]] > expect[S[win_start]]
                                || expect[S[win_start]] == 0) {
                            appear[S[win_start]]--;
                            win_start++;
                        }
                        if ((win_end - win_start + 1) < minWidth) {
                            minWidth = win_end - win_start + 1;
                            min_start = win_start;
                            //cout << "min_start	" <<min_start << endl;
                            //cout << "min_width	" <<minWidth<< endl;
                        }
                    }
                }
    
                if (minWidth == INT_MAX)
                    return "";
                else
                    return S.substr(min_start, minWidth);
    
    
            }
    };

    精简一下条件判断

    class Solution {
        public:
            string minWindow(string S, string T)
            {
                if(S.empty() || T.empty() || S.size() < T.size())
                    return string();
    
                vector<int> expect(256, 0);
                vector<int> appear(256, 0);
    
                for(int i = 0; i < T.size(); i++)
                {
                    expect[T[i]] ++;
                }
    
                int minWidth = INT_MAX, min_start = 0; 
                int win_start = 0;
                int appearCharCnt = 0;
                for(int win_end = 0; win_end < S.size(); win_end++)
                {   
                    appear[S[win_end]]++;
                    if(appear[S[win_end]] <= expect[S[win_end]])
                        appearCharCnt ++; 
                    //cout << "appearCharCnt	" <<appearCharCnt<< endl;
                    if(appearCharCnt == T.size())
                    {   
                        // shrink the win start
                        while (appear[S[win_start]] > expect[S[win_start]]
                              ) { 
                            appear[S[win_start]]--;
                            win_start++;
                        }   
                        if ((win_end - win_start + 1) < minWidth) {
                            minWidth = win_end - win_start + 1;
                            min_start = win_start;
                            //cout << "min_start	" <<min_start << endl;
                            //cout << "min_width	" <<minWidth<< endl;
                        }
                    }
                }
    
                if (minWidth == INT_MAX)
                    return "";
                else
                    return S.substr(min_start, minWidth);
    
    
            }
    };
  • 相关阅读:
    P2P技术简介(包括BT软件的分析)(转)
    为什么BT网络中迅雷的速度会这么快,比其它BT软件快
    迅雷中几个概念
    BT中的磁力链接(转)
    eclipse代码不能自动提示的问题解决
    eclipse不会自动编译的问题解决
    BT网络中DHT和UPnp的解释(转)
    BT服务器的搭建(tracker-P2P服务器架设)(转)
    BT原理分析(转)
    BT种子文件文件结构分析(转)
  • 原文地址:https://www.cnblogs.com/diegodu/p/4329944.html
Copyright © 2011-2022 走看看