最小子串覆盖 · 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 ...
随机推荐
- python3高阶函数:map(),reduce(),filter()的区别
转载请注明出处:https://www.cnblogs.com/shapeL/p/9057152.html 1.map():遍历序列,对序列中每个元素进行操作,最终获取新的序列 print(list( ...
- 【转】Linux中文件的可读,可写,可执行权限的解读以及chmod,chown,chgrp命令的用法
chmod是更改文件的权限 chown是改改文件的属主与属组 chgrp只是更改文件的属组. 一.文件权限解读 如上图所示,开头的-rwxrw-r--这一字符串标识文件权限. 这个字符串有10位,可以 ...
- 详解UML图之类图 (转)
原址: https://www.jianshu.com/p/4cd95d4ddb59 2. 怎么画类图?用什么工具? 使用工具:Visio或者processon在线作图 在类图中一共包含了以下几种模 ...
- bag of words
参考文献 Bag-of-words model (BoW model) 最早出现在NLP和IR领域. 该模型忽略掉文本的语法和语序, 用一组无序的单词(words)来表达一段文字或一个文档. 近年来, ...
- 第三方插件Vue-Lazyload
使图片懒加载
- CSS基础知识,学前准备
1.引入层叠样式表: A.行内引入 <bodystyle="background-color:#cccccc">; 在标签内使用style属性 </body> ...
- elasticsearch的插件安装
目前使用的是2.4.5版本的es 安装的时候注意以下几点 : 1.如果想所有的ip都能访问es,需要修改config下的elasticsearch.yml.修改如下 network.host=0.0. ...
- Ztree小demo用于系统授权
本示例只做到指定id用户的拥有的权限回显,并能动态获得ztree中重新选择的权限id.(至于权限的更新,就是后台人员对象和权限对象建立关系的过程,不做展示) 第一步:拼写jsp页面(下载ztree包, ...
- fusionjs 学习一 基本试用
参考demo 项目 https://github.com/rongfengliang/fusionjs-docker-demo 安装 create startkit yarn global add c ...
- 【转】Linux下的文本dos格式转Unix格式,去除^M符号
原文网址:http://blog.csdn.net/kobejayandy/article/details/13291525 问:我在Windows中通过FTP传一个文本文件到Linux中,但是打开文 ...