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 ...
随机推荐
- Meanshift均值漂移算法
通俗理解Meanshift均值漂移算法 Meanshift车手?? 漂移?? 秋名山??? 不,不,他是一组算法, 今天我就带大家来了解一下机器学习中的Meanshift均值漂移. Mea ...
- OpenAL音频库例程
Windows下C++可用的OpenAL demo. 基于alut工具库的OpenAL例程,涵盖了基本的OpenAL指令,对部分作出了注释,并且可以播放(当然得把对应的音频文件放到正确的路径下). # ...
- SpringCloud消息总线
我们在springcloud(七):配置中心svn示例和refresh中讲到,如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码发送请求来刷新客户端 ...
- 20145232韩文浩 《网络对抗技术》 Web基础
Apache 因为端口号80已经被占用(上次实验设置的),所以先修改/etc/apache2/ports.conf里的端口为5232后重新开启 可以在浏览器中输入localhost:5208来检查是否 ...
- POI对Excel的操作
1. 先导包 commons-io-2.6.jar包,用于对文件的操作. 下载地址:http://commons.apache.org/proper/commons-io/download_io.cg ...
- 不同后台服务器共用同一session
建一个类继承SessionStateStoreProviderBase类,override Initialize.SetAndReleaseItemExclusive.ReleaseItemExclu ...
- SQL 经典应用
SQL Server日常维护常用的一些脚本整理. 1.sql server开启clr权限: exec sp_configure 'clr enabled', 1 GO RECONFIGURE GO A ...
- html input file accept 上传文件类型限制格式 MIME 类型列表
例: <input type="file" accept="application/vnd.openxmlformats-officedocument.spread ...
- 在Spring-Boot中实现通用Auth认证的几种方式
code[class*="language-"], pre[class*="language-"] { background-color: #fdfdfd; - ...
- 每天学点SpringCloud(一):使用SpringBoot2.0.3整合SpringCloud
最近开始学习SpringCloud,在此把我学习的过程记录起来,跟大家分享一下,一起学习.想学习SpringCloud的同学赶快上车吧. 本次学习使用得SpringBoot版本为2.0.3.RELEA ...