minimum-window-substring (minimum region substring)

Disclaimer: This article is written self-study, pointed out that if the error also hope, thank you ^ - ^ https://blog.csdn.net/weixin_43871369/article/details/91453962

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

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

这道题的思路是:
1) begin开始指向0, end一直后移,直到begin - end区间包含T中所有字符。
记录窗口长度d
2) 然后begin开始后移移除元素,直到移除的字符是T中的字符则停止,此时T中有一个字符没被
包含在窗口,
3) 继续后移end,直到T中的所有字符被包含在窗口,重新记录最小的窗口d。
4) 如此循环知道end到S中的最后一个字符。
时间复杂度为O(n)
public class Solution {
    public String minWindow(String S, String T) {
        int[] map = new int[128];
        //init map, 记录T中每个元素出现的次数
        for(int i = 0; i < T.length(); i++) {
            map[T.charAt(i)]++;
        }
 
// begin end两个指针指向窗口的首位,d记录窗口的长度, counter记录T中还有几个字符没被窗口包含
        int begin = 0, end = 0, d = Integer.MAX_VALUE, counter = T.length(), head = 0;
        // end指针一直向后遍历
        while(end < S.length()) {
// map[] > 0 说明该字符在T中出现,counter-- 表示对应的字符被包含在了窗口,counter--, 如果s中的字符没有在T中出现,则map[]中对应的字符-1后变为负值
            if(map[S.charAt(end++)]-- > 0) {
                counter--;
            }
// 当counter==0时,说明窗口已经包含了T中的所有字符
            while (counter == 0) {
                if(end - begin < d) {
                    d = end - (head = begin);
                }
                if(map[S.charAt(begin++)]++ == 0) {  // begin开始后移,继续向后寻找。如果begin后移后指向的字符在map中==0,表示是在T中出现的,如果没有出现,map[]中的值会是负值。
                    counter++;                      // 在T中的某个字符从窗口中移除,所以counter++。
                }
            }
        }
        return d==Integer.MAX_VALUE ? "" :S.substring(head, head+d);
    }
}

 

//常规方法
#include <iostream>
#include<string>
#include<vector>
using namespace std;
  bool exist2(string str1,string str2)
    {
        int hash[128]={0};
        for(int i=0;i<str1.size();++i)
            ++hash[str1[i]];
        for(int i=0;i<str2.size();++i)
        {
            --hash[str2[i]];
            if(hash[str2[i]]<0) return false;
        }
        return true;
    }
    int subc(string obj,string subs)
    {
        for(int i=subs.size();i<=obj.size();++i)
         if(exist2(obj.substr(0,i),subs)) return i;
        return -1;
    }
    string minWindow(string S, string T) {
        if(S.size()<T.size()) return "";
        vector<string>temp;
        int index;
        for(int i=0;i<S.size()-T.size()+1;++i)
        {
            if(T.find(S[i])!=string::npos)
            {
                if(subc(S.substr(i,S.size()),T)!=-1)
                {
                    index=subc(S.substr(i,S.size()),T);
                    if(i==0||i+index==S.size()) temp.push_back(S.substr(i,i+index));
                    else temp.push_back(S.substr(i,i+index-1));
                }
            }
        }
        if(temp.empty()) return "";
        index=0;
        for(int i=1;i<temp.size();++i)
          if(temp[i].size()<temp[index].size())
              index=i;
        return temp[index];
    }
int main()
{
    string s1,s2;
    cin>>s1>>s2;
    cout<<minWindow(s1,s2)<<endl;
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_43871369/article/details/91453962