leetcode — minimum-window-substring
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的更多相关文章
- [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]Minimum Window Substring @ Python
原题地址:https://oj.leetcode.com/problems/minimum-window-substring/ 题意: Given a string S and a string T, ...
- [LeetCode] Minimum Window Substring 散列映射问题
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- 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] 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 超时,但觉得很清晰。
我的超时思路,感觉自己上了一个新的台阶,虽然超时了,但起码是给出了一个方法. 遍历s 一遍即可,两个指针,当找到了一个合格的字串后,start 开始走,直到遇到s[start]在t中 如果不符合,en ...
- 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 ...
- 53. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
随机推荐
- Swagger2基本注解使用
@Api:用在请求的类上,表示对类的说明 tags="说明该类的作用,可以在UI界面上看到的注解" value="该参数没什么意义,在UI界面上也看到,所以不需要配置&q ...
- [小结]InnoDB体系结构及工作原理
参阅:<innodb存储引擎内幕>原创文章,会不定时更新,转发请标明出处:http://www.cnblogs.com/janehoo/p/7717041.html 一.概述: innod ...
- mongoDB根据_id进行查询
var ObjectID = require('mongodb').ObjectID; whereStr = {_id:ObjectID(req.body._id)}
- 第2周个人作业:WordCount
Github地址: https://github.com/hddddd/Wordcount 1.PSP表格 PSP2.1 PSP阶段 预估耗时(分钟) 实际耗时(分钟) Planning 计划 30 ...
- SQL给数据编号
方法:ROW_NUMBER() over(order by getdate()) AS num 使用案例 : select * From (select ROW_NUMBER() over(orde ...
- EF学习笔记(十) 处理并发
总目录:ASP.NET MVC5 及 EF6 学习笔记 - (目录整理) 上一篇:EF学习笔记(九):异步处理和存储过程 本篇原文链接:Handling Concurrency Concurrency ...
- hdu 4370
这个题说实话我没看出来,我看的别人的博客 https://blog.csdn.net/u013761036/article/details/39377499 这个人讲的很清楚,可以直接去看他的 题目给 ...
- Shell变量类型和运算符-2
- 段的性能统计信息v$segment_statistics
v$segment_statistics视图记录了段的统计信息 简单的几个字段就不说了,就说最后三个吧 STATISTIC_NAME,STATISTIC#,VALUE记录了发生在表上的操作 SYS @ ...
- [转]kaldi ASR: DNN训练
作者:zqh_zy链接:http://www.jianshu.com/p/c5fb943afaba來源:简书著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 本文通过简单kaldi ...