最小子串覆盖 · Minimum Window Substring
[抄题]:
给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串。
在答案的子串中的字母在目标字符串中是否需要具有相同的顺序?
——不需要。
给出source = "ADOBECODEBANC",target = "ABC" 满足要求的解 "BANC"
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
原来哈希算法不仅仅是哈希表,int 256数组也可以被称作哈希
[一句话思路]:
boys & girls窗口型两指针
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- targethash 和 sourcehash在此处都是比较一共出现了多少次,而不是对应位置上是否相等。所以0-256所有的字符都要比
- 存所有的字母c和存数字0-256效果相同
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[关键模板化代码]:
for (i = 0; i < source.length(); i++) {
//accumulate if not valid
while(j < source.length() && !valid(sourcehash, targethash)) {
sourcehash[source.charAt(j)]++;
j++;
}
//change if valid
if (valid(sourcehash, targethash) && (j - i) < ans) {
ans = Math.min(ans, j - i);
minStr = source.substring(i,j);
}
//back to i + 1
sourcehash[source.charAt(i)]--;
}
j在i中量变、质变
[总结]:
结果是两个256哈希数组进行比较,如果字母次数不如target就不符合
[复杂度]:Time complexity: O(2n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
567. Permutation in String 字符串中字母相等:用指针
Substring with Concatenation of All Words 字符串中字母相等:用指针
[代码风格] :
.substring 单独一个独立的方法,应该都用小写
public class Solution {
/**
* @param source : A string
* @param target: A string
* @return: A strindenote the minimum window, return "" if there is no such a string
*/
void initTargetHash (int[] targethash, String target) {
for (char ch: target.toCharArray()) {
targethash[ch]++;
}
}
public boolean valid(int[] sourcehash, int[] targethash) {
for (int i = 0; i < 256; i++) {
if (sourcehash[i] < targethash[i]) {
return false;
}
}
return true;
}
public String minWindow(String source , String target) {
//corner case
String minStr = "";
if (source.length() < target.length()) {
return minStr;
}
int[] sourcehash = new int[256];
int[] targethash = new int[256];
int i = 0, j = 0;
int ans = Integer.MAX_VALUE;
//initialization
initTargetHash (targethash, target);
//i,j go in the same direction
for (i = 0; i < source.length(); i++) {
//accumulate if not valid
while(j < source.length() && !valid(sourcehash, targethash)) {
sourcehash[source.charAt(j)]++;
j++;
}
//change if valid
if (valid(sourcehash, targethash) && (j - i) < ans) {
ans = Math.min(ans, j - i);
minStr = source.substring(i,j);
}
//back to i + 1
sourcehash[source.charAt(i)]--;
}
//return
return minStr;
}
}
最小子串覆盖 · Minimum Window Substring的更多相关文章
- LeetCode 76. 最小覆盖子串(Minimum Window Substring)
题目描述 给定一个字符串 S 和一个字符串 T,请在 S 中找出包含 T 所有字母的最小子串. 示例: 输入: S = "ADOBECODEBANC", T = "ABC ...
- lintcode 中等题:minimum window substring 最小子串覆盖
题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...
- Lintcode--004(最小子串覆盖)
给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 注意事项 如果在source中没有这样的子串,返回"",如果有多个 ...
- leetcode76. Minimum Window Substring
leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
- LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram
1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...
- 【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 ...
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- 53. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
随机推荐
- 词云:解决pip install wordcloud安装过程中报错“error: command 'x86_64-linux-gnu-gcc' failed with exit status 1”问题
外部环境:ubuntu16.04, 64bits, 全局环境python2.7 在虚拟环境(python3.5)中执行 pip install wordcloud 时安装失败,报错: error: c ...
- Winform开发常用控件之TreeView菜单导航和权限用法
TreeView一个很棒的控件,我们在做WEB开发时常常犯困的一个东东.当然这里介绍winform里面的用法唠. 先介绍几个属性吧,CheckBoxes设置为true的话树形节点前面会出现checkb ...
- BZOJ1835: [ZJOI2010]base 基站选址【线段树优化DP】
Description 有N个村庄坐落在一条直线上,第i(i>1)个村庄距离第1个村庄的距离为Di.需要在这些村庄中建立不超过K个通讯基站,在第i个村庄建立基站的费用为Ci.如果在距离第i个村庄 ...
- spring-security-4 (3)spring security过滤器的创建与注册原理
spring security是通过一个过滤器链来保护你的web应用安全.在spring security中,该过滤链的名称为springSecurityFilterChain,类型为FilterCh ...
- Python的安装和使用ubuntu下
ubuntu下Python的安装和使用 https://www.cnblogs.com/luckyalan/p/6703590.html ubuntu下Python的安装和使用 1 文章介绍 本文介绍 ...
- L3-012 水果忍者 (30 分)
2010年风靡全球的“水果忍者”游戏,想必大家肯定都玩过吧?(没玩过也没关系啦~)在游戏当中,画面里会随机地弹射出一系列的水果与炸弹,玩家尽可能砍掉所有的水果而避免砍中炸弹,就可以完成游戏规定的任务. ...
- Oracle联合查询
select * from teacher--联合查询 --01.union (并集)select tno from teacher where tno>1080 union(select tn ...
- mongodb启用Profiling定位问题
建议使用方法三,最简单且容易查看 一:如果mongodb已经运行了很长时间,此时查看mongod.log很大,没法打开查看相应信息 #ps -ef|grep mongod 找到相应的mongod的进程 ...
- pthread访问调用信号线程的掩码(pthread_sigmask )
掩码: 信号掩码 在POSIX下,每个进程有一个信号掩码(signal mask).简单地说,信号掩码是一个"位图",其中每一位都对应着一种信号.如果位图中的某一位为1,就表示在执 ...
- 采用轻量ServiceMesh实现灰度发布的实践
软件总会有缺陷的,解决问题的同时往往会引入新的问题,关键是看这些问题是否在我们的控制范围内,“灰度发布”就是让问题受控的方法之一. 前言 我们的 CTO 经常说:“研发团队最首要的任务是提供稳定的服务 ...