首页 > 数据库 >[Oracle] LeetCode 76 Minimum Window Substring 双指针

[Oracle] LeetCode 76 Minimum Window Substring 双指针

时间:2022-09-26 05:44:05浏览次数:50  
标签:substring cnt string min len Substring 76 Window ans

Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "".

The testcases will be generated such that the answer is unique.

A substring is a contiguous sequence of characters within the string.

Solution

我们首先用 \(map\) 来记录 \(t\) 中字符出现的次数。然后用左右指针分别移动

点击查看代码
class Solution {
private:
    string ans="";
    unordered_map<char,int> mp;
    int min_len=INT_MAX;
    int st_ans=0;
public:
    string minWindow(string s, string t) {
        int m = s.size(), n = t.size();
        if(m<n)return ans;
        int l=0,h=0;
        int cnt=0;
        for(auto ele:t)mp[ele]++;
        for(h=0;h<m;h++){
            if(mp[s[h]]>0)cnt++;//means that this letter is in t
            mp[s[h]]--;
            if(cnt==n){
                while(l<h && mp[s[l]]<0){mp[s[l]]++;l++;}
                if(min_len>h-l+1){
                    min_len = h-l+1;st_ans=l;
                }
                mp[s[l]]++;l++;
                cnt--;
            }
        }
        return min_len==INT_MAX?"":s.substr(st_ans,min_len);
    }
};

标签:substring,cnt,string,min,len,Substring,76,Window,ans
From: https://www.cnblogs.com/xinyu04/p/16729587.html

相关文章

  • 弃用 Windows,政府机构 5000 万台电脑将替换为国产 Linux!
    来源:https://www.linuxmi.com/50-million-pc-linux.html开源社区的一大胜利!继德国之后,中国现在想在5000万台PC上抛弃Windows并运行Linux!如果您一直密切关注Lin......
  • Specified key was too long; max key length is 767 bytes错误的原因
    将mysql数据库里某个UNIQUE唯一索引字段从utf8改为utf8mb4时提示1071-Specifiedkeywastoolong;maxkeylengthis767bytes,来看看这个错误的来原因。来几个知识点......
  • win11删除Windows.old方法
    win11删除Windows.old方法在win11中,系统由于更新和安装会留下Windows.old缓存文件,非常占用内存,这时候我们可以通过磁盘清理的方式来删除win11下的Windows.old文件夹。......
  • 【windows】在windows右键菜单加入在当前路径打开cmd功能?
    在Ubuntu中可以在一般目录下点击右键选中OpeninTerminal即可打开一个命令终端,由于自己平常在windows上开发时也常常使用cmd命令行进行操作,但是每次都需要提前复制好要访......
  • windows编程之MessageBox
    windows编程之MessageBox什么是MessageBoxMessageBox是一个函数,用于显示一个模态对话框,其中包含一个系统图标、一组按钮和一个简短的特定于应用程序消息,如状态或错误......
  • 用VS Code搞Qt 6:Gui基础类型——QGuiApplication和QWindow
    在99.996%的情况下,我们弄Qt应用都会使用QApplication类和QWidget类,即直接用Widgets库中的组件/控件。为了方便开发人员自己造轮子,Qt也提供了一套基础的GUI组件......
  • 为什么obj的方法属性里,this会指向Window?
    昨天遇到的一道题,问输出结果是什么?为什么?如何更改可以输出另一个name?varname="Tom"varobj={name:"Jerry",getName:function(){functionget(......
  • windows系统自动启动wsl中sshd等后台服务
     windows系统自动启动wsl中sshd等后台服务 通常,在wsl中启动sshd等后台服务,可以在wsl中用/etc/init.d/sshstart启动sshd服务(前提是sshd已经设置好,能正常启动服务),但关......
  • windows自动关机脚本.vbs
    Dimmytime,myout1,myout2mytime=2'mytime=InputBox("请输入定时时间(格式20:10:05)"&vbLf&"如果想倒计时关机,请输入倒计时时间"&vbLf&"(单位/分钟)"&vbLf......
  • windows上linux子系统wsl的启用和管理
     windows上linux子系统wsl的启用和管理1、windows可以安装多个wsl分发子系统,包括kali-linux、ubuntu等,这些子系统可以同时运行,数据各自独立。2、wsl子系统安装好了后,可......