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.

思路:题目中T可能会有重复的字母 我用了各种分类讨论 结果超时

直接看大神的代码吧

class Solution {
public:
string minWindow(string S, string T) {
int m = S.size(), n = T.size();
if (n <= || m < n) return ""; int require[] = {}; //记录每种字母需要多少个 关键点
for (int i = ; i < n; ++i) require[T[i]]++; int count = ;
int minLen = INT_MAX, minIndex = ;
for (int s = , e = ; e < m; ++e) {
require[S[e]]--; //末尾数字的需求量减1
if (require[S[e]] >= ) count++; //如果需求量大于等于0 说明匹配上了新的数字
while (count == n) { //所有字母都被匹配上了
if (e - s + < minLen) { //长度变小了 记录下新的长度 和 起始位置
minLen = e - s + ;
minIndex = s;
}
require[S[s]]++; //起始位置向后移更新需求量
if (require[S[s]] > ) count--;
s++;
}
} if (minLen == INT_MAX) return "";
return S.substr(minIndex, minLen);
}
};

【leetcode】Minimum Window Substring (hard) ★的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. 【leetcode刷题笔记】Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  3. 【LeetCode练习题】Minimum Window Substring

    找出包含子串的最小窗口 Given a string S and a string T, find the minimum window in S which will contain all the ...

  4. [LeetCode] 76. Minimum Window Substring 解题思路

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  5. 【LeetCode】159. Longest Substring with At Most Two Distinct Characters

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...

  6. 【LeetCode】Longest Palindromic Substring 解题报告

    DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...

  7. [LeetCode] 76. Minimum Window Substring 最小窗口子串

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  8. [Leetcode][JAVA] Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  9. Java for LeetCode 076 Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

随机推荐

  1. [设计模式] javascript 之 组合模式

    组合模式说明 组合模式用于简单化,一致化对单组件和复合组件的使用:其实它就是一棵树: 这棵树有且只有一个根,访问入口,如果它不是一棵空树,那么由一个或几个树枝节点以及子叶节点组成,每个树枝节点还包含自 ...

  2. POJ 2635 The Embarrassed Cryptographer

    大数取MOD... The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1 ...

  3. CSS样式案例(2)-制作一个简单的登录界面

    首先来张完工的效果图. 一.html文件如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  4. java系列-使用maven创建web项目(二)

    推荐2个maven找jar包配置的网站,只需要搜索关键字即可找到需要的Jar包,非常方便,比如:MySQL就可以找到mysql-connect-Java.jar. http://search.mave ...

  5. 【PHP面向对象(OOP)编程入门教程】16.__toString()方法

    我们前面说过在类里面声明“__”开始的方法名的方法(PHP给我们提供的),都是在某一时刻不同情况下自动调用执行的方 法,“__toString()”方法也是一样自动被调用的,是在直接输出对象引用时自动 ...

  6. PHP的$_SERVER['PHP_SELF']造成的XSS漏洞攻击及其解决方案

    $_SERVER['PHP_SELF']简介 $_SERVER['PHP_SELF'] 表示当前 PHP文件相对于网站根目录的位置地址,与 document root 相关. 假设我们有如下网址,$_ ...

  7. sql种类

  8. Gunicorn 问题

    Does Gunicorn suffer from the thundering herd problem? The thundering herd problem occurs when many ...

  9. HackerRank training-the-army

    Description 有 \(n\) 个技能,每次可以通过一个巫师,将一个技能转化成另一个技能,问最有最多有多少不同的技能. Sol 网络流. 先说说我一开始非常 naive 的建图,将技能拆点,中 ...

  10. php判断浏览器语言

    <?php // 分析 HTTP_ACCEPT_LANGUAGE 的属性 // 这里只取第一语言设置 (其他可根据需要增强功能,这里只做简单的方法演示) preg_match('/^([a-z\ ...