[抄题]:

给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串。

在答案的子串中的字母在目标字符串中是否需要具有相同的顺序?

——不需要。

给出source = "ADOBECODEBANC",target = "ABC" 满足要求的解  "BANC"

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

原来哈希算法不仅仅是哈希表,int 256数组也可以被称作哈希

[一句话思路]:

boys & girls窗口型两指针

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. targethash 和 sourcehash在此处都是比较一共出现了多少次,而不是对应位置上是否相等。所以0-256所有的字符都要比
  2. 存所有的字母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的更多相关文章

  1. LeetCode 76. 最小覆盖子串(Minimum Window Substring)

    题目描述 给定一个字符串 S 和一个字符串 T,请在 S 中找出包含 T 所有字母的最小子串. 示例: 输入: S = "ADOBECODEBANC", T = "ABC ...

  2. lintcode 中等题:minimum window substring 最小子串覆盖

    题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...

  3. Lintcode--004(最小子串覆盖)

    给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 注意事项 如果在source中没有这样的子串,返回"",如果有多个 ...

  4. leetcode76. Minimum Window Substring

    leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...

  5. Minimum Window Substring @LeetCode

    不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...

  6. 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 ...

  7. 【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 ...

  8. 刷题76. Minimum Window Substring

    一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...

  9. 53. Minimum Window Substring

    Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...

随机推荐

  1. [UOJ213][UNR #1]争夺圣杯

    uoj description 一个长为\(n\)的序列,给定一个参数\(m\),求所有长度为\(m\)的区间的最大值之和. 对于所有的\(m\in[1,n]\)你都需要分别求出答案然后异或起来. \ ...

  2. 禅道导出数据,excel打开为乱码处理

    禅道里面导出的数据,用Excel打开是乱码(如图),如何解决? 第一步: 第二步: 第3步: 第4步: 第5步: 选择一个储存位置 最后的结果就是这样了

  3. 如何查看 ThinkPHP5.1 的升级说明

    如何查看 ThinkPHP5.1 的升级说明 ThinkPHP 官方对于升级历史都有说明,这个官方做的非常不错. 在官方的手册中就有. 比如从 ThinkPHP 5.1.26 升级到 ThinkPHP ...

  4. CentOS6.6 VSFTP服务器安装设置

    1:安装vsftpd    yum install vsftpd 2:关闭防火墙 service iptables stop 3:允许21端口通行 vi /etc/sysconfig/iptables ...

  5. 3.Appium运行时出现:Original error: Android devices must be of API level 17 or higher. Please change your device to Selendroid or upgrade Android on your device

    参考博客:https://blog.csdn.net/niubitianping/article/details/52624417 1.错误信息:Original error: Android dev ...

  6. 玩转Panabit 2008 Live CD到U盘,Panabit随身行

    直接将Panabit 2008 Live CD的内容复制到U盘,即把Live CD转成U盘启动盘,U盘可写,方便保存配置.要能正常使用,必须机器支持USB启动,才能用上:但是熟悉此方法,同样可以灵活用 ...

  7. Python——内置函数(待完善)

    内置函数(68个),分为六大类 思维导图: 1. 迭代器/生成器相关(3个) (1)range for i in range(10): #0-9 print(i) for i in range(1,1 ...

  8. yii2 memcache 跨平台交互 键和值不一样

    1 首先在配置文件中加载 web\basic\config\web.php ........ 'components' => [ 'request' => [ // !!! insert ...

  9. 剪贴板增强---Kawvin增强剪贴板_V2.0

    #Persistent SetWorkingDir,%A_ScriptDir% ;设置工作目录 #MaxThreadsPerHotkey ;最大热键数量 #NoEnv ;#Warn #SingleIn ...

  10. Fiddler 抓包工具总结-bpafter

    转摘https://www.cnblogs.com/shy1766IT/p/5199334.html Fiddler 抓包工具总结   名称 含义 # 抓取HTTP Request的顺序,从1开始,以 ...