[LC] 76. Minimum Window Substring
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的更多相关文章
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- 【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 ...
- [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 ...
- 76. Minimum Window Substring
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- 【一天一道LeetCode】#76. Minimum Window Substring
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- 关于Java的String字符串常量的长度问题
虽然这种问题应该很难遇到,但是遇到了也会感到莫名其妙.不知道大家有没有遇到那种在java代码里用字符串写sql语句的情况,但是如果sql语句字符串的长度太长的话就会报错. 代码如下: 代码A Stri ...
- Unix网络编程学习 < 一 >
#include "unp.h" int main(int argc , char**argv) { int sockfd , n; //sockfd套接字描述符 ]; struc ...
- 安装lombok插件IDEA的插件栏加载不出来
打开 Setting-->Appearance & Behavior -->Syetem Setting -->Updates,将Use secure connection ...
- 洛谷 P1060开心的金明
题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NN元钱就行”.今 ...
- POJ 1860 Currency Exchange【bellman-Ford模板题】
传送门:http://poj.org/problem?id=1860 题意:给出每两种货币之间交换的手续费和汇率,求出从当前货币s开始交换回到s,能否使本金增多. 思路:bellman-Ford模板题 ...
- cppcheck下载及使用
一.参考文档 1.Ubuntu下安装Cppcheck源码操作步骤 2.cppcheck std.cfg not found error when std.cfg file is available 3 ...
- Python与mongo交互
# 导入模块 import pymongo # 连接MongoDB数据库 conn = pymongo.MongoClient('localhost', 27017) # 建库 db = conn.g ...
- java使用forEach填充字典值
// 填充字典值 Vector vector = vectorMapper.selectByPrimaryKey(id); VectorModel vectorModel = new VectorMo ...
- c/c++基础 输入函数/流
....光看算法了没怎么注意输入函数输入流 无论get(char *) gets(char *)/gets_s(char * ,sizeof char*) cin.getline(char* ,siz ...
- 2×c列联表|多组比例简式|卡方检验|χ2检验与连续型资料假设检验
第四章 χ2检验 χ2检验与连续型资料假设检验的区别? 卡方检验的假设检验是什么? 理论值等于实际值 何条件下卡方检验的需要矫正?如何矫正? 卡方检验的自由度如何计算? Df=k-1而不是n-1 卡方 ...