题目

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.

分析

在字符串 S 中查找最小的窗口,使窗口中全部包含字符串T 中的字符,(不按顺序)。

需要注意的地方:

  1. T 中的字符可以重复,窗口需要重复包含。
  2. 测试 实验中的是SCII字符 ,所以可以用128位数组代替 hash table。

AC代码

class Solution {
public:
string minWindow(string S, string T) {
if (S.empty() || T.empty())
{
return "";
}
int count = T.size();
int require[128] = { 0 };
bool chSet[128] = { false };
for (int i = 0; i < count; ++i)
{
require[T[i]]++;
chSet[T[i]] = true;
}
int i = -1;
int j = 0;
int minLen = INT_MAX;
int minIdx = 0;
while (i < (int)S.size() && j < (int)S.size())
{
if (count)
{
i++;
require[S[i]]--;
if (chSet[S[i]] && require[S[i]] >= 0)
{
count--;
}
}//if
else
{
if (minLen > i - j + 1)
{
minLen = i - j + 1;
minIdx = j;
}
require[S[j]]++;
if (chSet[S[j]] && require[S[j]] > 0)
{
count++;
}
j++;
}//else
}//while
if (minLen == INT_MAX)
{
return "";
}
return S.substr(minIdx, minLen);
}
};

GitHub测试程序源码

LeetCode(76) Minimum Window Substring的更多相关文章

  1. LeetCode(5)Longest Palindromic Substring

    题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...

  2. LeetCode(111) Minimum Depth of Binary Tree

    题目 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the s ...

  3. LeetCode(64) Minimum Path Sum

    题目 Total Accepted: 47928 Total Submissions: 148011 Difficulty: Medium Given a m x n grid filled with ...

  4. LeetCode(76): 最小覆盖子串

    Hard! 题目描述: 给定一个字符串 S 和一个字符串 T,请在 S 中找出包含 T 所有字母的最小子串. 示例: 输入: S = "ADOBECODEBANC", T = &q ...

  5. [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 ...

  6. 【LeetCode】76. Minimum Window Substring

    Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...

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

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

  8. Minimum Window Substring @LeetCode

    不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...

  9. 刷题76. Minimum Window Substring

    一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...

随机推荐

  1. iOS 手写输入法奔溃,替换隐藏键盘方法

    {   UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:se ...

  2. css学习笔记(8)

    1.元素使用了float以后就脱离了文档流,不能通过margin:0 auto;来居中了. 2.position:relative;定位后可以通过margin:0 auto;来居中,也说明了relat ...

  3. 转:LoadRunner负载测试之Windows常见性能计数器,分析服务器性能瓶颈

    发布于2012-10-8,来源:博客园 监测对象 System(系统) l %Total Processor Time 系统中所有处理器都处于繁忙状态的时间百分比,对于多处理器系统来说,该值可以反映所 ...

  4. C++学习基础九——继承

    1.不同于Java中通过extends实现继承,C++是通过:实现的. 2.C++中同样包含public,private,protected三个关键字: public关键字表示在任意其他类中可调用该成 ...

  5. swift 当协议遇见了泛型

    由于泛型比较简单,并没有单独拿出来介绍!我们在定义函数的时候,有时候只是由于参数或者返回值类型不同,而具体的实现过程是一模一样的,这个时候我们就可以定义泛型函数而使可以传入不同的参数类型: func ...

  6. angular学习笔记,很乱哈哈。

    1.鼠标悬浮出现的信息v-bind:title="message" 2.对该便签进行结果判断显示隐藏v-if=''控制台设置 app3.seen = false(消失).控制台设置 ...

  7. 关于yaha中文分词(将中文分词后,结合TfidfVectorizer变成向量)

    https://github.com/jannson/yaha # -*- coding: utf-8 -*- """ Created on Wed Aug 10 08: ...

  8. thinkphp加载 和url_model

    1.加载thinkphp.php requrie('./ThinkPHP/ThinkPHP.php'); 2.加载核心文件 ./thinkPHP/LIB/core 3.加载项目的文件 分析URL 调用 ...

  9. VC++ 在两个程序中 传递字符串等常量值的方法:使用了 WM_COPYDATA 消息的

    消息作用:    在进程间共享数据(内部通过创建内存映射文件) 消息介绍:需要用到的数据结构/类型:typedef struct tagCOPYDATASTRUCT {    ULONG_PTR dw ...

  10. 6.Mybatis中的动态Sql和Sql片段(Mybatis的一个核心)

    动态Sql是Mybatis的核心,就是对我们的sql语句进行灵活的操作,他可以通过表达式,对sql语句进行判断,然后对其进行灵活的拼接和组装.可以简单的说成Mybatis中可以动态去的判断需不需要某些 ...