import java.util.HashMap;
import java.util.Map; /**
*
* Source : https://oj.leetcode.com/problems/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).
*
* For example,
* S = "ADOBECODEBANC"
* T = "ABC"
*
* Minimum window is "BANC".
*
* Note:
*
* > If there is no such window in S that covers all characters in T,
* return the emtpy string "".
*
* > If there are multiple such windows, you are guaranteed that there
* will always be only one unique minimum window in S.
*
*/
public class MinimumWindowSubstring { /**
* 寻找S中包含T的最短字符串
*
* 窗口的方法,
* 先将T中所有字符保存在hash表中,值为每个字符出现的次数
* 从头开始遍历,判断该字符是否在hash表中,如果在,则将该字符对应的值减1,并记录此时已包含T中字符总数
* 如果字符总数等于T的长度说明找到一个子串,找到第一个包含T的字符串,然后将此时子串的长度和之前最小长度比较,更新最小字串的长度,并记录此时子串的起始位置
* 然后移动子串的左边界,略过不再T中的字符
* 当遍历完成的时候,记录的最小字串的起始位置和最小字串的长度可以得到包含T的最小字串
*
* @param S
* @param T
* @return
*/
public String findMinimumWindowSubString (String S, String T) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
// 初始化hash表
for (int i = 0; i < T.length(); i++) {
if (map.keySet().contains(T.charAt(i))) {
map.put(T.charAt(i), map.get(T.charAt(i)) + 1);
} else {
map.put(T.charAt(i), 1);
}
} int minLen = S.length();
int left = 0;
int minStart = 0;
int count = 0;
for (int i = 0; i < S.length(); i++) {
Character ch = S.charAt(i);
if (map.keySet().contains(ch)) {
map.put(ch, map.get(ch) - 1);
if (map.get(ch) >= 0) {
count ++;
}
// 如果找到一个子串
while (count == T.length()) {
if (i - left + 1 < minLen) {
minLen = i - left + 1;
minStart = left;
}
if (map.keySet().contains(S.charAt(left))) {
// 如果右移的时候遇到了T中的字符,将hash表中对应字符加1,因为在后面要查找该字符
map.put(S.charAt(left), map.get(S.charAt(left)) + 1);
if (map.get(S.charAt(left)) > 0) {
// 如果加1之后小于1,说明之前是负数,说明现在窗口内还有多个T中的字符,需要继续向前移动窗口
// 如果大于1才停止向前移动窗口
count--;
}
}
left ++;
}
}
}
if (minLen > S.length()) {
return "";
}
return S.substring(minStart, minLen + minStart); } public static void main(String[] args) {
MinimumWindowSubstring minimumWindowSubstring = new MinimumWindowSubstring();
System.out.println(minimumWindowSubstring.findMinimumWindowSubString("ADOBECODEBANC", "ABC"));
}
}

leetcode — minimum-window-substring的更多相关文章

  1. [LeetCode] Minimum Window Substring 最小窗口子串

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  2. [leetcode]Minimum Window Substring @ Python

    原题地址:https://oj.leetcode.com/problems/minimum-window-substring/ 题意: Given a string S and a string T, ...

  3. [LeetCode] Minimum Window Substring 散列映射问题

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  4. Leetcode Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  5. [Leetcode] minimum window substring 最小字符窗口

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  6. LeetCode()Minimum Window Substring 超时,但觉得很清晰。

    我的超时思路,感觉自己上了一个新的台阶,虽然超时了,但起码是给出了一个方法. 遍历s 一遍即可,两个指针,当找到了一个合格的字串后,start 开始走,直到遇到s[start]在t中 如果不符合,en ...

  7. Minimum Window Substring @LeetCode

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

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

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

  10. 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. final,static,abstract

    一.final 1.可以修饰属性,表示常量,其值在运行期间不允许改变.常量必须赋初始值,初始值可以在声明出,构造方法的任意一个地方赋 优点:增强程序的可读性,可维护性 2.可以修饰方法,表示该方法不能 ...

  2. 使用pdf.js预览实现读取服务器外部文件

    不知道大家使用百度网盘的文件预览功能,f12看过控制台没有. 发现百度网盘使用的预览文件功能全是基于开源pdf .js的 接下来正题,我们在使用pdf.js默认是读取发布容器内部的文件,读取外部的文件 ...

  3. mysql利用LAST_INSERT_ID实现id生成器

    首先了解 LAST_INSERT_ID LAST_INSERT_ID 有自己的存储空间,能存一个数字 不带参数时返回最近insert的那行记录的自增字段值.带参数时会将自己存储的数字刷成参数给定的值 ...

  4. Linux 下 ftp的使用

    最近需要在Linux上搭建FTP服务,通过网上的一些大神学习了一些新知识,在这个做一个总结: Linux 下FTP 为 vsftp (linux red hat)1.FTP配置路径:/etc/vsft ...

  5. sqlserver 多行转一行

    sql 例子: SELECT STUFF((SELECT ',' + CONVERT(VARCHAR, b.SCsinfoSourceId) FROM PZDataCsinfo b WHERE b.D ...

  6. Codeforces Round #418 (Div. 2)

    A: 不细心WA了好多次 题意:给你一个a序列,再给你个b序列,你需要用b序列中的数字去替换a序列中的0,如果能够替换,则需要判断a是否能构成一个非递增的序列,a,b中所有的数字不会重复 思路:就是一 ...

  7. FPGA中带优先级的if else if与不带优先级的case的探讨

    我们知道在书本上都说让我们尽量使用不带优先级的的数据选择器,今天我们就来探讨一下二者的区别. 例子1:带优先级的的数据选择器,综合成功,且没有任何警告. module detection_prio # ...

  8. bash基础特性3(shell编程)

    Linux上文本处理三剑客: grep:文本过滤工具 sed:stream editor,文本编辑工具 awk:文本报告生成器 grep -v:显示不能够被pattern匹配到的行 -i:忽略字符大小 ...

  9. MFC单文档视图程序简介

    在视图应用程序中,应用程序的数据由文档对象代表,数据的视图由视图对象代表.MFC的Cdocument类是文档对象的基类,Cview类是视图对象的基类.应用程序的主窗口,其操作功能在MFC的Cframe ...

  10. 背水一战 Windows 10 (105) - 通知(Toast): 带按钮的 toast, 带输入的 toast(文本输入框,下拉选择框)

    [源码下载] 背水一战 Windows 10 (105) - 通知(Toast): 带按钮的 toast, 带输入的 toast(文本输入框,下拉选择框) 作者:webabcd 介绍背水一战 Wind ...