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. String 字符串,heredoc,nowdoc

    一个字符串可以用 4 种方式表达: 单引号 双引号 heredoc 语法结构 nowdoc 语法结构(自 PHP 5.3.0 起) 单引号 定义一个字符串的最简单的方法是用单引号把它包围起来(字符 ' ...

  2. 《打造扛得住的MySQL数据库架构》第4章 MySQL数据库结构优化

    4-1 数据库结构优化介绍 良好的数据库逻辑设计和物理设计是数据库获得高性能的基础. 1.减少不必要的数据冗余. 2.尽量避免数据维护中出现更新,插入和删除异常. 插入异常:如果表中的某个实体随着另一 ...

  3. 201771010123汪慧和《面向对象程序设计JAVA》第八周实验总结

    一.理论部分 1.接口 用interface声明,是抽象方法和常量值定义的集 合.从本质上讲,接口是一种特殊的抽象类. 在Java程序设计语言中,接口不是类,而是对类 的一组需求描述,由常量和一组抽象 ...

  4. Linux 常用命令全拼

    pwd: print work directory 打印当前目录 显示出当前工作目录的绝对路径 ps: process status(进程状态,类似于windows的任务管理器) 常用参数:-auxf ...

  5. 洛谷P1002 过河卒(动态规划)

    题目描述 棋盘上 AA 点有一个过河卒,需要走到目标 BB 点.卒行走的规则:可以向下.或者向右.同时在棋盘上 CC 点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为 ...

  6. 洛谷 P1060开心的金明

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NN元钱就行”.今 ...

  7. 物体检测中常用的几个概念迁移学习、IOU、NMS理解

    1.迁移学习 迁移学习也即所谓的有监督预训练(Supervised pre-training),我们通常把它称之为迁移学习.比如你已经有一大堆标注好的人脸年龄分类的图片数据,训练了一个CNN,用于人脸 ...

  8. 配置Action

    配置Action 实现了Action类后,就可以在struts.xml中配置该Action类了.配置Action就是让Struts2 知道哪个Action处理哪个请求,也就是完成用户请求和Action ...

  9. nouveau :failed to create kernel chanel,-22

    一:錯誤描述:今天在重啓 Ubuntu 的過程中,出現下圖的 grub 選項,系統重啓/開機之後出現以下畫面,然後選擇 Ubuntu 之後黑屏,提示錯誤:nouveau :failed to crea ...

  10. python编程:从入门到实践----第五章>if 语句

    一.一个简单示例 假设有一个汽车列表,并想将其每辆汽车的名称打印出来.遇到汽车名‘bmw’,以全大写打印:其他汽车名,首字母大写 cars=['audi','bmw','subaru','toyota ...