题目

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. [ActionScript 3.0] AS3.0 烟雾粒子效果

    package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Sprite; ...

  2. css学习笔记(8)

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

  3. 山东ACM省赛历届入口

    山东省第一届ACM大学生程序设计竞赛 山东省第二届ACM大学生程序设计竞赛 山东省第三届ACM大学生程序设计竞赛 山东省第四届ACM大学生程序设计竞赛 山东省第五届ACM大学生程序设计竞赛 山东省第六 ...

  4. resignFirstResponder

    What is this resignFirstResponder business? Here is the short version:Some view objects are also con ...

  5. 九、DAG hierarchy

    DAG 节点有两种,Transformation/shape. shape节点是transformation的子节点. transformation节点包括position, rotation, sc ...

  6. 二模02day1解题报告

    T1.淘汰赛制 比赛时的淘汰赛制,给出每两个球队比赛的胜率,求出最终胜率最高的队伍. 这题的概率真的很难算啊感觉...一开始打的代码打下来就是用f[i][j]表示i场比赛后第j人还在场的概率.不难看出 ...

  7. ziparchiver添加后编译出错

    Build setting里面compile source as改为Objective-c

  8. VBS在指定范围内生成不重复的随机数

    Dim Z()ReDim Z(15)For i=0 To UBound(Z)    Z(i)=GetRndNum(i-1,UBound(Z))    WScript.Echo Z(i)Next Fun ...

  9. 常用vim设置

    set tabstop=4set shiftwidth=4set expandtabset hlsearchset cindent set autoindent set tabstop=4是设TAB宽 ...

  10. List集合转换为数组形式

    通过List集合对象的转类型函数 .ToArray() 如List<decimal> → deciaml[] 代码如下: var ComIdList = ComList.Select(p ...