题目

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. JavaScript 输入内容就触发事件

    <textarea name="textarea" placeholder="请输入产品识别号" onkeyup="alert();" ...

  2. gerrit集成gitweb后,点击gitweb连接:not found(转载)

    From:http://blog.sina.com.cn/s/blog_4fb490ff01018i0v.html 需要添加refs/meta/config的read access权限.

  3. ASP.NET中进行消息处理(MSMQ) 二(转)

          在我上一篇文章<ASP.NET中进行消息处理(MSMQ)一>里对MSMQ做了个通俗的介绍,最后以发送普通文本消息和复杂的对象消息为例介绍了消息队列的使用. 本文在此基础上继续介 ...

  4. svn 提交失败

    刚刚使用SVN 提交代码时提示失败. svn: Commit failed (details follow):svn: Can't open file '/home/svn/project/db/tx ...

  5. ZYB's Premutation POJ5592

    Problem Description ZYBZYBZYB has a premutation PPP,but he only remeber the reverse log of each pref ...

  6. ActionScript 3.0 for the Lunder Algorithm

    package com.feiruo.Calendar.LunderCalendar { /* *@ClassName: package:com.feiruo.Calendar.LunderCalen ...

  7. 慕课网-安卓工程师初养成-5-4 使用 Eclipse 调试程序

    来源:http://www.imooc.com/video/1627 IDE断点调试功能 比如 之前的程序,写错了,变成如下 package com.imooc; import java.util.S ...

  8. php判断 !==false

    测试 if($res !== false){ echo "未定义通过<br>"; }else{ echo "未定义不通过<br>"; } ...

  9. 关于DCOM的安全性

    关于DCOM的安全性 DCOM的安全性设置在注册表中. 2. 通过DCOMCNF.exe可以配置

  10. 1-9 TCP/IP参考模型

    ISO/OSI参考模型与TCP/IP模型对比 一.网络访问层 功能:包括IP地址与物理硬件地址的映射以及将IP地址封装成帧. 基于不同类型的网络接口,网路访问层定义了和物理介质的连接 网路访问层包含了 ...