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.

思路:问题可以转变为,T中各个字符的数量<= window中的这些字符的数量,所以用map纪录字符与字符数量的关系

class Solution {
public:
bool ifContain(map<char,int>& source, map<char,int>& target) //check if S contains T
{
for(map<char,int>::iterator it = target.begin(); it != target.end(); it++) //map的遍历
{
if(source[it->first] < it->second) return false;
}
return true;
} string minWindow(string S, string T) {
int minLength = INT_MAX;
int start = , end = , minStart = , minEnd = ;
map<char,int> source;
map<char,int> target;
source[S[start]]++; //map的插入[法I]source[key]=value; [法II]source.insert(make_pair(key,value));
for(int i = ; i< T.length(); i++)
{
target[T[i]]++;
}
while()
{
if(ifContain(source, target)){
if(end-start+ < minLength)
{
minStart = start;
minEnd = end;
minLength = end-start+;
if(minLength == T.size()) return S.substr(minStart,minLength);
}
source[S[start]]--; //寻找更小的窗口
start++;
}
else //不包含,则扩大窗口
{
end++;
if(end==S.size()) break;
source[S[end]]++;
}
}
if(minLength>S.size()) return "";
else return S.substr(minStart,minLength);
}
};

76. Minimum Window Substring (String, Map)的更多相关文章

  1. 刷题76. Minimum Window Substring

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

  2. 【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 ...

  3. 76. Minimum Window Substring

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  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]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. 76. Minimum Window Substring(hard 双指针)

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

  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. 76. Minimum Window Substring (JAVA)

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

  9. [LC] 76. Minimum Window Substring

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

随机推荐

  1. Scrum立会报告+燃尽图(3)选题

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2193 一.小组介绍 组长:刘莹莹 组员:朱珅莹 孙韦男 祝玮琦 王玉潘 ...

  2. c++类成员函数重载常量与非常量版本时避免代码重复的一种方法

    c++有时候需要为类的某个成员函数重载常量与非常量的版本,定义常量版本是为了保证该函数可作用于常量类对象上,并防止函数改动对象内容.但有时两个版本的函数仅仅是在返回的类型不同,而在返回前做了大量相同的 ...

  3. python 安装psutil包报错:

    报错: Failed building wheel for psutil Google得知,需要安装python-devel 和 wheel sudo dnf install python-devel ...

  4. c语言 判断文件是否存在

    使用access函数 功能: 检查调用进程是否可以对指定的文件执行某种操作. 用法: #include <unistd.h> #include <fcntl.h> int ac ...

  5. cratedb joins 原理(官方文档)

      JOINs are essential operations in relational databases. They create a link between rows based on c ...

  6. zookeeper命名服务

    zookeeper概念 zooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,底层组成单元是znode,对于zookeeper来说,所有的功能都是基于znode来实现的,因此有万物皆节点 ...

  7. .NET泛型解析(上)

    [1]:泛型介绍 泛型是C#2.0中一个重要的新特性,泛型是CLR和编程语言提供的一种特殊机制,它支持另一种形式的代码重用.泛型通常用与集合以及作用于集合的方法一起使用,当然也可以单独使用. C#是一 ...

  8. [您有新的未分配科技点]可,可,可持久化!?------0-1Trie和可持久化Trie普及版讲解

    这一次,我们来了解普通Trie树的变种:0-1Trie以及在其基础上产生的可持久化Trie(其实,普通的Trie也可以可持久化,只是不太常见) 先简单介绍一下0-1Trie:一个0-1Trie节点只有 ...

  9. 02 - Unit02:登录功能

    需求实现步骤 发送Ajax请求 服务器处理 Ajax回调处理 登录功能 发送Ajax请求 绑定事件:"登录"按钮的单击事件 获取参数:用户名userName和密码password ...

  10. Oracle.DataAccess.dll 部署安装

    Oracle.DataAccess.dll 要拷贝到项目发布目录 项目发布的时候,还必须要拷贝以下几个文件在运行目录1.oci.dll 2.oraociicus11.dll 3.OraOps11w.d ...