首页 > 其他分享 >76. Minimum Window Substring

76. Minimum Window Substring

时间:2023-03-07 13:03:51浏览次数:40  
标签:end string int while Substring 76 Window window 字符串

76. Minimum Window Substring

标签(空格分隔): leetcode hard


题目

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.

思路

本题是典型的子字符串求解问题。对于大多数子字符串问题,我们得到一个字符串,需要找到满足某些限制条件的子字符串。一般的方法是使用一个包含两个指针的hashmap。模板如下所示。

int findSubstring(string s){
        vector<int> map(128,0);
        int counter; // check whether the substring is valid
        int begin=0, end=0; //two pointers, one point to tail and one  head
        int d; //the length of substring

        for() { /* initialize the hash map here */ }

        while(end<s.size()){

            if(map[s[end++]]-- ?){  /* modify counter here */ }

            while(/* counter condition */){ 
                 
                 /* update d here if finding minimum*/

                //increase begin to make it invalid/valid again
                
                if(map[s[begin++]]++ ?){ /*modify counter here*/ }
            }  

            /* update d here if finding maximum*/
        }
        return d;
  }

本题的解决方案就呼之欲出了
Note:需要提到的一点是,当要求找到最大子字符串时,我们应该在内部while循环之后更新最大值,以保证子字符串是有效的。另一方面,当要求找到最小子字符串时,我们应该在内部while循环中更新最小值。


代码
class Solution {
public:
    string minWindow(string s, string t) {
        //定义变量count,头尾指针begin及end
        int count=t.size(),begin=0,end=0,head=0,d=INT_MAX;
        //利用ASCII码来代表字符并进行存储
        vector<int> map(128,0);
        for(auto item:t)
            map[item]++;
        //移动头尾指针开始寻找最小子串
        while(end<s.size())
        {
            if(map[s[end++]]-->0)
                count--;
            while(count==0)
            {
                //比较是否为最短子串
                if((end-begin)<d) d =end-(head=begin);
                if(map[s[begin++]]++==0)
                    count++;
            }
        }
        return d==INT_MAX?"":s.substr(head,d);        
    }
};

标签:end,string,int,while,Substring,76,Window,window,字符串
From: https://blog.51cto.com/u_15996214/6105920

相关文章

  • Windows下CMake 中使用 pkg-config
    #set(PKG_CONFIG_EXECUTABLE"F:/vcpkg/packages/pkgconf_x64-windows/tools/pkgconf/pkgconf.exe")#set(PKG_CONFIG_USE_CMAKE_PREFIX_PATHON)set(PKG_CONFIG_ARGN"......
  • Windows10 删除Windows.edb,释放C盘空间
    运行win10系统一段时间后,发现电脑非常卡顿,检查后发现Windows.edb文件占用内存比拟大。Windows.edb一个Window搜索服务数据库文件,索引后提供文件,内容和属性缓存的搜索结果......
  • Windows clash
    ClashforWindows教程一、Clash简介ClashforWindows是运行在Windows上的一图形化Clash分支。支持SS/VMess/VLESS/Trojan/Snell/NaiveProxy协议等。二......
  • Windows上Spotless的回车换行符问题
    在Windows上执行命令“mvncleanpackage-DskipTests”编译时报如下错误,原因是Windows默认为“\r\n”。解决办法是在仓库的根目录下创建名为“.gitattributes”的文件......
  • 更改windows桌面路径的教程
    第一步:键盘上按住"win+E"打开文件资源管理器,然后快速访问的桌面,点击“属性”。第二步:默认桌面在用户名下的Desktop文件夹,比如:C:\Users\ataola\Desktop,在注册表的路径为......
  • Windows系统中卸载文件系统
    1.查看挂载的盘符netuse 2.卸载X盘符挂载的文件系统netuseX:/delete 3.其它手动卸载Windows系统中所有已挂载的文件系统netuse*/delete自动卸载Wi......
  • tabby美观且实用的终端工具(windows/macos版 ,亲测有效!!!)
    Tabby简介Tabby(前身是Terminus)是一个可高度配置的终端模拟器和SSH或串口客户端,支持Windows,macOS和Linux功能强大到爆:集成SSH,Telnet客户端和连接管理器集成串......
  • Windows Server 2003 安装 python
    WindowsServer2003是32位的系统,最高支持的python版本是3.4下载python-3.4https://www.python.org/ftp/python/3.4.4/python-3.4.4.msi D:\Python34\Scripts>pip......
  • golang获取windows版本和详细信息
    场景:将木马丢到感染机运行后回连时希望返回感染机的操作系统信息.golang可以通过runtime.OS获取到操作系统类型,但是无法获取详细的版本信息,如win7win10等,解决方案;......
  • windows内核网络调试
    1windows网络调试2bcdedit/dbgsettingsnethostip:192.168.2.1port:500003Key=sfz54lfnnz7r.2qv9aiovadd5i.2gtkz3xamru32.cdwwl45caxfl456bcdedit/set{......