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).

Example:

Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"

Note:

  • If there is no such window in S that covers all characters in T, return the empty string "".
  • If there is such window, you are guaranteed that there will always be only one unique minimum window in S.

Idea 1. Sliding window with two pointers. The right pointer to expand the window while fixing the left window and the left pointer to shrink the given window while fixing the right window. At any time only one of these pointer move and the other remains fixed. 经典的滑动窗口的方法, 用左右2各点滑动形成的窗口中找寻符合条件的窗口。这题麻烦的地方是如何快速的判断符合条件的窗口(letterSeen == T.length),包含T的所有字母的最小窗口, 包含T的所有字母的窗口字母个数和T至少一样长。T中可能有重复字母,需要用map来记下每个字母出现的个数。

如何update letterSeen?

  ++letterSeen if window[right] <= letterT[right] after ++window[right]

  --letterSeen if window[left] < letterT[left] after --window[left]

Time complexity: O(S + T), in the worst case each element is visited twice, once by right pointer, once by the left pointer. (exactly loop time 2 * S + T)

Space complexity: O(T + S), depends on the dictionary size

1.a.建立2个map, 第一个letterT用来记下T中字母出现个数,第二个window记录滑动窗口中字母出现的个数, 判断窗口中出现的字母是不是符合条件的,比如s = "aab", t = "ab", letterT['a' - 'A'] = 1,  当right = 0, window['a'-'A'] = 1 <= 2, T中的字母扫了一个,letterSeen = 1; 当right = 1, window['a'-'A'] = 2 > letterT['a' - 'A'], 扫到的比T中还多,算是多余的,就不能加letterSeen, letterSeen = 1; 当right = 2,window['b'-'A'] = 1 <= letter['b'-'A'], letterSeen = 2 == T.length, "aab"是第一个符合条件的窗口;再固定右边活动左边来找是不是还有更小的,window['a'-'A'] = 1, letterSeen == T.length, "ab"符合条件; 继续右滑,window['a'-'A'] = 0 < T['a' - 'A'], letterSeen = 1 不符合条件。

++letterSeen if window[c-'A'] <= T[c-'A']

Note.

  window[c-'A']用作字母为key的map很方便

滑动左边不要忘记 ++left

 class Solution {
public String minWindow(String s, String t) {
int[] letterT = new int[256];
for(int i = 0; i < t.length(); ++i) {
++letterT[t.charAt(i) - 'A'];
} int[] window = new int[256];
int start = -1;
int letterSeen = 0;
int minLen = Integer.MAX_VALUE;
for(int left = 0, right = 0; right < s.length(); ++right) {
int keyRight = s.charAt(right) - 'A';
++window[keyRight];
if(window[keyRight] <= letterT[keyRight]) {
++letterSeen;
while(letterSeen == t.length()) {
if(right - left + 1 < minLen) {
minLen = right - left + 1;
start = left;
}
int keyLeft = s.charAt(left) - 'A';
--window[keyLeft];
if(window[keyLeft] < letterT[keyLeft]) {
--letterSeen;
}
++left;
}
}
} if(start == -1) {
return "";
}
return s.substring(start, start + minLen);
}
}

python

 class Solution:
def minWindow(self, s: str, t: str) -> str:
letterT = collections.Counter(t) window = collections.defaultdict(int)
left = 0
letterSeen = 0
start = -1
minLen = len(s) + 1
for right in range(len(s)): window[s[right]] += 1
if window[s[right]] <= letterT[s[right]]:
letterSeen += 1
while letterSeen == len(t):
if right - left + 1 < minLen:
start = left;
minLen = right - left + 1 window[s[left]] -= 1
if window[s[left]] < letterT[s[left]]:
letterSeen -= 1 left += 1 if start == -1:
return "" return s[start: start + minLen]

2.b 只需要一个map letterT, 用右滑动来减字母个数,左滑动来加字母个数,如果左边也扫到尾部,正好左右抵消还原原来的map.

 class Solution {
public String minWindow(String s, String t) {
int[] letterT = new int[256]; for(int i = 0; i < t.length(); ++i) {
++letterT[t.charAt(i) - 'A'];
} int lettersSeen = 0;
int minLen = Integer.MAX_VALUE;
int start = -1;
for(int left = 0, right = 0; right < s.length(); ++right) {
--letterT[s.charAt(right) - 'A'];
if(letterT[s.charAt(right) - 'A'] >= 0) {
++lettersSeen; while(lettersSeen == t.length()) {
if(right - left + 1 < minLen) {
minLen = right - left + 1;
start = left;
}
++letterT[s.charAt(left) - 'A'];
if(letterT[s.charAt(left) - 'A'] > 0) {
--lettersSeen;
}
++left;
}
}
} if(start == -1) {
return "";
}
return s.substring(start, start + minLen);
}
}

python:

 class Solution:
def minWindow(self, s: str, t: str) -> str:
letterT = collections.Counter(t) lettersSeen = 0
start = -1
minLen = len(s) + 1
left = 0
for right in range(len(s)):
if s[right] in letterT:
letterT[s[right]] -= 1
if letterT[s[right]] >= 0:
lettersSeen += 1 while lettersSeen == len(t):
if right - left + 1 < minLen:
minLen = right - left + 1
start = left if s[left] in letterT:
letterT[s[left]] += 1 if letterT[s[left]] > 0:
lettersSeen -= 1 left += 1 if start == -1:
return "" return s[start: start+minLen]

Minimum Window Substring LT76的更多相关文章

  1. 53. Minimum Window Substring

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

  2. Minimum Window Substring @LeetCode

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

  3. LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram

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

  4. leetcode76. Minimum Window Substring

    leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...

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

  6. 刷题76. Minimum Window Substring

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

  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. [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. HashMap、LinkedHashMap、ConcurrentHashMap、ArrayList、LinkedList 底层实现

    HashMap相关问题 1.你用过HashMap吗?什么是HashMap?你为什么用到它? 用过,HashMap是基于哈希表的Map接口的非同步实现,它允许null键和null值,且HashMap依托 ...

  2. cdnbest的站点日志保存时间怎么设置

    站点的保存时间是以节点全局里的保存时间为准 站点的日志保存大小还不能设置

  3. SQL Server子查询填充DataSet时报500内部错误的解决办法

    运行环境为Visual Studio 2010,数据库为SQL Server 2008. 执行下面SQL语句 SELECT SubsiteId, SubsiteTitle, count(Collect ...

  4. jxls2 java.lang.NegativeArraySizeException

    某个单元格使用jx:each报这个错误. 内部代码,创建一个二维数组时,传了一个负值进去 解决办法 将这个单元格删除,重新编写批注

  5. 【Scrapy】关于使用Scrapy框架爬虫遇到的问题1

    class testScrapy(scrapy.Spider): name = "testLogs" allowed_domains=["cnblogs.com" ...

  6. php中in_array一些问题

    var_dump(in_array(1,['s','1fsdf',12])); // true var_dump(in_array(0,[4,'erdd']));  // true 因为是要用值与数组 ...

  7. ceph luminous 新功能之内置dashboard 之 mgr功能模块配置

    前言 ceph luminous版本新增加了很多有意思的功能,这个也是一个长期支持版本,所以这些新功能的特性还是很值得期待的,从底层的存储改造,消息方式的改变,以及一些之前未实现的功能的完成,都让ce ...

  8. 【nginx】大文件下载

    nginx自带文件读取功能,而且实现地很好. 比如直接读取txt文件,png图片等,用chrome可以直接获取到内容. 但是对于很大的文件,比如有2个G的视频,nginx如何吐出2G的内容呢? 实验: ...

  9. 并行网络爬虫(C++实现)

    step1 使用socket编程技术,利用http协议,抽取网页中的url,实现简单的爬虫. socket int socket (int domain, int type, int protocol ...

  10. Java 的静态工厂方法

    本文转载自:https://www.jianshu.com/p/ceb5ec8f1174 序:什么是静态工厂方法 Effective Java 2.1 静态工厂方法与构造器不同的第一优势在于,它们有名 ...