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.

Time: O(N)

class Solution:
def minWindow(self, s: str, t: str) -> str:
res = ''
my_dict = {}
for char in t:
freq = my_dict.get(char, 0)
my_dict[char] = freq + 1
count, start, end, min_len = len(my_dict), 0, 0, sys.maxsize
res_end, res_start = 0, 0
while end != len(s):
char = s[end]
if char in my_dict:
my_dict[char] -= 1
if my_dict[char] == 0:
count -= 1
end += 1 while count == 0:
start_char = s[start]
if start_char in my_dict:
my_dict[start_char] += 1
if my_dict[start_char] > 0:
count += 1 if min_len > end - start:
min_len = end - start
res_end = end
res_start = start
start += 1 return s[res_start: res_end]
class Solution {
public String minWindow(String s, String t) {
Map<Character, Integer> map = new HashMap<>();
int minLen = Integer.MAX_VALUE;
char[] tCharArray = t.toCharArray();
for (char c : tCharArray) {
map.put(c, map.getOrDefault(c, 0) + 1);
} int count = map.size(), start = 0, globStart = 0, i = 0;
char[] sCharArray = s.toCharArray();
while (i < sCharArray.length) {
char cur = sCharArray[i];
if (map.containsKey(cur)) {
map.put(cur, map.get(cur) - 1);
if (map.get(cur) == 0) {
count -= 1;
}
}
i += 1;
while (count == 0) {
char sChar = sCharArray[start];
if (map.containsKey(sChar)) {
map.put(sChar, map.get(sChar) + 1);
if (map.get(sChar) > 0) {
count += 1;
}
}
if (i - start < minLen) {
globStart = start;
minLen = i - start;
}
start += 1;
}
}
return minLen == Integer.MAX_VALUE ? "" : s.substring(globStart, globStart + minLen);
}
}

[LC] 76. Minimum Window Substring的更多相关文章

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

  4. 76. Minimum Window Substring

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

  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最小字符串窗口

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

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

  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. 【一天一道LeetCode】#76. Minimum Window Substring

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. 翻译——3_Gaussian Process Regression

    使用不同的机器学习方法进行预测 续上篇2_Linear Regression and Support Vector Regression 高斯过程回归 %matplotlib inline impor ...

  2. go语言使用

    设置 package control 在 Preferences->Package Setting->Package Control->Settings - User 中加入 cha ...

  3. MySQL--事务,隔离性和隔离级别

    事务 事务就是一组数据库操作,要么全部执行成功,要么全部执行失败,在MySQL中,事务是依靠存储引擎层实现的. ACID(Atomicity,Consistency,Isolation,Durabil ...

  4. JavaScript数组去重(5种方法)

    // 数组去重 let arr = ['a', 'b', 'b', 1, 1, 'true', true, true, NaN, NaN, 'NaN', undefined, undefined, n ...

  5. Centos7.6部署rsyslog+loganalyzer+mysql日志管理服务器

    参考来自: the_script :https://yq.aliyun.com/articles/675198 名山.深处:https://www.cnblogs.com/skychenjiajun/ ...

  6. ES6 find()

    Array.prototype.find() 返回数组中满足提供测试函数的第一个元素的值,否则返回undefined let b = blogs.find(function(e) => { re ...

  7. Hard Disk Drive(MBR)

    这里讲的主要是网上所谓的老式磁盘,它是由一个个盘片组成的,我们先从个盘片结构讲起.如图1所示,图中的一圈圈灰色同心圆为一条条磁道,从圆心向外画直线,可以将磁道划分为若干个弧段,每个磁道上一个弧段被称之 ...

  8. Hibernate(四)--延迟加载(lazyload)

    hibernate中的延迟加载(lazyload)分属性的延迟加载和关系的延迟加载 属性的延迟加载: 当使用load的方式来获取对象的时候,只有访问了这个对象的属性,hibernate才会到数据库中进 ...

  9. 金蝶cloud成本核算流程

  10. vim下看C++代码

    看C++代码, 缺少合适的编辑器,捣鼓vim. 安装Vundle, 用于插件管理 git clone https://github.com/VundleVim/Vundle.vim.git ~/.vi ...