zoukankan      html  css  js  c++  java
  • 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 empty string "".

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

    class Solution {
    public:
        string minWindow(string s, string t) {
            unordered_map<char,int> m;
            for(char c : t){
                m[c]++;
            }
            int sL = s.length();
            int counter = t.length();
            int start = 0,end = 0;
            int minStart = 0,minLen = INT_MAX;
            
            while(end < sL){
                if(m[s[end]]>0){
                    counter--;
                }
                m[s[end]]--;
                end++;
                while(counter == 0){
                    if(end-start < minLen){
                        minStart = start;
                        minLen = end-start;
                    }
                    m[s[start]]++;
                    if(m[s[start++]] > 0){
                        counter++;
                    }
                }
            }
            if(minLen < INT_MAX)
                return s.substr(minStart,minLen);
            else
                return "";
            
        }
    };
  • 相关阅读:
    iOS_核心动画(二)
    iOS_核心动画CALayer(一)
    iOS_KVC与KVO
    iOS_Quartz 2D绘图
    iOS_触摸事件与手势识别
    iOS_多线程(二)
    iOS_多线程(一)
    iOS_UIAlertController
    CTF-Pwn-[BJDCTF 2nd]diff
    数据结构--队列(Java实现)
  • 原文地址:https://www.cnblogs.com/wxquare/p/5878117.html
Copyright © 2011-2022 走看看