[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 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.
题意:
给定字符串S 和 T, 求S中可以cover T所有元素的子集的最小长度。
Solution1: Two Pointers(sliding window)
1. scan String T, using a map to record each char's frequency
2. use [leftMost to i] to maintain a sliding window, making sure that each char's frequency in such sliding window == that in T
3. if mapS [S.charAt(start)] > mapT [S.charAt(start)] , it signs we can move sliding window
4. how to find the next sliding window? move leftMost, meanwhile, decrement mapS [S.charAt(start)] until we find each frequency in [start to i] == that in T
code
class Solution {
public String minWindow(String S, String T) {
String result = "";
if (S == null || S.length() == 0) return result;
int[] mapT = new int[256];
int[] mapS = new int[256];
int count = 0;
int leftMost = 0;
for(int i = 0; i < T.length(); i++){
mapT[T.charAt(i)] ++;
} for(int i = 0; i < S.length(); i++){
char s = S.charAt(i);
mapS[s]++;
if(mapT[s] >= mapS[s]){
count ++;
} if(count == T.length()){
while(mapS[S.charAt(leftMost)] > mapT[S.charAt(leftMost)]){
if(mapS[S.charAt(leftMost)] > mapT[S.charAt(leftMost)]){
mapS[S.charAt(leftMost)]--;
}
leftMost ++;
}
if(result.equals("") || i - leftMost + 1 < result.length()){
result = S.substring(leftMost, i+1);
}
}
}
return result;
}
}
二刷:
对于出现在S但不出现在T的那些“配角” character的处理,
最好的方式是,边扫S边用map将其频率一并记上。
这样,在判断 while(mapS[S.charAt(leftMost)] > mapT[S.charAt(leftMost)]) 这个逻辑的时候,
这些“配角”character会因为只出现在S但不出现在T
而直接被left++给做掉
class Solution {
public String minWindow(String s, String t) {
String result = "";
if(s == null || s.length() == 0 || s.length() < t.length()) return result; int [] mapT = new int [128];
for(Character c : t.toCharArray()){
mapT[c]++;
} int left = 0;
int count = t.length();
int[] mapS = new int[128];
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
mapS[c] ++ ;
if(mapT[c] >= mapS[c]){
count --;
}
if(count == 0){
while(mapS[s.charAt(left)] > mapT[s.charAt(left)]){
mapS[s.charAt(left)] --;
left++;
}
if (result.equals("") || i - start + 1 < result.length()) {
result = s.substring(start, i + 1);
}
}
}
return result;
}
}
[leetcode]76. Minimum Window Substring最小字符串窗口的更多相关文章
- [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] 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 ...
- Leetcode#76 Minimum Window Substring
原题地址 用两个指针分别记录窗口的左右边界,移动指针时忽略那些出现在S种但是没有出现在T中的字符 1. 扩展窗口.向右移动右指针,当窗口内的字符即将多于T内的字符时,停止右移 2. 收缩窗口.向右调整 ...
- [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
- 刷题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] 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- Python3 Flask+nginx+Gunicorn部署(上)
前言:一般在本地运行flask项目通常是直接python3 文件名.py,然后打开:http://127.0.0.1:5000 查看代码结果 这次主要是记录flask在python3 环境结合ngin ...
- day 41 标准文档流 浮动
一.标准文档流 什么是标准文档流 宏观的将,我们的web页面和ps等设计软件有本质的区别,web 网页的制作,是个“流”,从上而下 ,像 “织毛衣”.而设计软件 ,想往哪里画东西,就去哪里画 标准文档 ...
- TF(1): 基础理论
TensorFlow最初由Google大脑的研究员和工程师开发出来,用于机器学习和神经网络方面的研究,于2015.10宣布开源,在众多深度学习框架中脱颖而出,在Github上获得了最多的Star量.T ...
- ID的故事
随心所欲.这个时代比较中二吧,刚出国,也买了房,年纪轻轻的觉得自己好像很牛B的样子. 失败悲观的路人甲.大约是13年的时候,突遭重击,一下子悲观失望,死的心都有.为此买了那种自杀也会给赔偿的保险(买后 ...
- 兼容ie,火狐的判断回车键js脚本
var event = window.event || arguments.callee.caller.arguments[0]; var keycode = event.keyCode || eve ...
- Spring Boot之执行器端点(Actuator Endpoint)实现剖析
整体实现思路是将端点(Endpoint)适配委托给MVC层策略端点(MvcEndpoint),再通过端点MVC适配器(EndpointMvcAdapter)将端点暴露为HTTP请求方式的MVC端点,最 ...
- MOBA英雄AI设计分享
转自:http://www.gamelook.com.cn/2018/07/333877 文/wataloo 1 设计概要 1.1 设计原则和目的 英雄AI的目的主要有: 1.新手过渡局,让玩家刚 ...
- Vue 封装的loading组件
<template> <div class="loadEffect"> <span></span> <span>< ...
- JVM-字节码
- 面试回顾——session相关
原地址:https://blog.csdn.net/quiet_girl/article/details/50580095 Session结束生命周期的几种情况: (1)客户端关闭浏览器(只针对ses ...