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

76. Minimum Window Substring [Hard]

时间:2023-01-29 14:58:25浏览次数:38  
标签:curMap curChar temp headChar expectMap Substring 76 Window result

76. Minimum Window Substring

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.

Constraints:

  • m == s.length
  • n == t.length
  • 1 <= m, n <= 10^5
  • s and t consist of uppercase and lowercase English letters.

Example

Input: s = "ADOBECODEBANC", t = "ABC"
Output: "BANC"
Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.

思路

  • 并不是需要连续字符串,只是要找出符合预期字符串出现字符个数的子串
  • 拆解预期字符串,得出每个字符有多少个,每有一个字符串,就可以认为是一个需要满足的条件
  • 看当前子串是否符合条件个数,不符合就继续加字符进来,如果已经符合条件了,就开始从左开始减字符个数,找到最小的子串

题解

    public String minWindow(String s, String t) {
        HashMap<Character, Integer> expectMap = new HashMap<>();
        HashMap<Character, Integer> curMap = new HashMap<>();
        int left, expectCount, curCount;
        String result, temp;
        result = temp = "";
        left = curCount = 0;

        // 先拿到期望字符串的各个字符个数
        for (char c : t.toCharArray())
            expectMap.put(c, expectMap.getOrDefault(c, 0) + 1);
        // 期望字符数
        expectCount = expectMap.size();

        for (int right = 0; right < s.length(); right++) {
            char curChar = s.charAt(right);
            temp += curChar;

            if (expectMap.containsKey(curChar)) {
                curMap.put(curChar, curMap.getOrDefault(curChar, 0) + 1);
                if (curMap.get(curChar).equals(expectMap.get(curChar)))
                    curCount++;
            }

            while (curCount == expectCount) {
                if (result.equals(""))
                    result = temp;
                else
                    result = result.length() > temp.length() ? temp : result;

                char headChar = s.charAt(left);
                if (expectMap.containsKey(headChar)) {
                    curMap.put(headChar, curMap.get(headChar) - 1);
                    if (expectMap.get(headChar) > curMap.get(headChar))
                        curCount--;
                }

                temp = temp.substring(1);
                left++;
            }
        }
        return result;
    }

标签:curMap,curChar,temp,headChar,expectMap,Substring,76,Window,result
From: https://www.cnblogs.com/tanhaoo/p/17047032.html

相关文章

  • Windows与Linux的互相访问
    安装上几台windows的主机,不管是xp系统还是win7win10,只要打开网络发现,在网上邻居上都可以看到局域网内在线的计算机。但是Linux就不行了。常见的解决办法是在Linux下安装S......
  • 如何获取Windows应用程序列表
    Windows任务管理器的应用程序栏包含任务窗口的列表。要获取此列表,窗口必须满足以下几个条件:(1)必须可见(2)包含一个标题(3)不能被其他窗口包含下面我给出源程序和调用示......
  • 利用火绒随意关停windows的defender
     共两步: 第一步:    第二步:   ......
  • windows server 2012 R2 内存占用过高优化
    现象:windowsserver 2012R2 使用中,任务管理器经常显示占用内存>96%,将所有进程占用内存加起来并没有占到系统内存这么多分析办法:使用RAMMap查看了机器内存使用情况,如......
  • 记录windows2012部署asp项目
    安装mysql5.6安装本地环境.NET4.6.1环境安装IIS部署......
  • 手电筒上架亚马逊UL1576测试报告
    一、办理UL测试报告的流程是什么?1、咨询报价2、签订合同3、填写申请表、并将样品和有关技术文件送至机构。5、支付测试费用。6、安排产品进行测试。7、如果试验不合格,机构将......
  • windows11预览版装WSA心得
    windows11预览版装WSA心得这两天心血来潮想要装个WSA(安卓windows子系统),原来一直用的安卓模拟器(mumu啊蓝叠啊逍遥啊),但感觉像wsa这种安卓系统与主系统融合的模式更带感,于......
  • 重装系统及Windows软件设置流程记录
    前言本文主要是记录给小白(女朋友)重装系统的流程,包括重装系统使用的工具和进入新系统后的软件配置。一、重装系统1.1启动盘工具ventoy可以满足一个U盘安装多个系统,下载......
  • 提高程序员工作效率的工具合集windows+ios
    提示:集合各种程序员必备工具,望学习收藏~文章目录​​前言​​​​一、Markdowm​​​​1:菜单栏​​​​2:文件​​​​3:编辑​​​​4:段落​​​​5:格式​​​​6:视图​​​......
  • Qt里的QSoundEffect在Linux和Windows平台上的差异
    最近写一个morse码练习软件,使用Qt开发,用到了QSoundEffect。因为Qt跨平台的特性,把Linux下的源代码直接放到Windows下编译可以直接通过,但运行起来却有问题。在Linux下节奏正......