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. JavaScript 之 Function

    JavaScript function 语句定义和用法: function 语句用于声明一个函数. 函数声明后,我们可以在需要的时候调用. 在 JavaScript 中,函数是对象,函数也有属性和方法 ...

  2. LeetCode——221. 最大正方形

    在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 暴力法 ...

  3. jboss的JVMroute记录

    jboss5的nodename是在 /usr/local/jboss-5.1.0.GA/server/dms/deploy/jbossweb.sar/server.xml   这里的  jvmrout ...

  4. 双向链表的双向冒泡排序 c++

    #include<iostream> using namespace std; #define swap(a,b) {int t;t = a;a = b;b = t;} //节点类型的定义 ...

  5. idea代理上网

    idea 代理上网 浏览器能够上网,idea无法下载jar 浏览器无法上网则配置浏览器代理 --------- start //------------------------浏览器代理完毕 idea ...

  6. Git&GitHub 基本使用

    如果是Windows用户需要自行去Git的官网下载Git工具 相信以您的聪明才知不需要一会就可以安装好Git工具了 博主这里是MacOS系统,本省就自带Git工具就不演示安装了 在完成一个简单的Git ...

  7. linux设置网络三种方法

    http://blog.csdn.net/u010003835/article/details/52233296

  8. 01 语言基础+高级:1-6 集合_day03【List、Set、Collections工具类】

    day03 [List.Set.数据结构.Collections] 主要内容 数据结构 List集合 Set集合 Collections 第一章 数据结构 2.1 数据结构有什么用 我们将常见的数据结 ...

  9. PAT Advanced 1033 To Fill or Not to Fill (25) [贪⼼算法]

    题目 With highways available, driving a car from Hangzhou to any other city is easy. But since the tan ...

  10. 中国文化产业基金越来越多,但IP变现难题为何仍未解决?

    自始至终,中国商界领域的态势就有一个很明显的特征--哪里是风口.哪里是热点,企业就会蜂拥而至并集体掘金.一直到决出胜负,或者把整个风口"做烂"才罢休.很典型的案例就是电商领域已经呈 ...