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 ...
随机推荐
- Java基础之一
移位操作符 移位操作符只可用来处理整数类型. <<:左移位操作符,按照操作符右侧指定的位数将操作符左边的操作数向左移动,在低位补0. >>:“有符号”右移位操作符,按照操作符右 ...
- vuex的getters处理数据
getters是用来处理state里的数据的 getters传递一个值state 例子: store.js import Vue from 'vue' import Vuex from 'vuex' ...
- Github使用:使用github用作自己的免费域名
1.创建一个新仓库 --- 删除里面的文件 --- Git上传文件到新仓库(必须有index.html) 2. 点击新仓库的setting,下滑找到GitHub Pages ---- 点击第一行的链接 ...
- 滚动公告--jq
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Python开发——10.面向对象编程进阶
一.isinstance(obj,cls)和issubclass(sub,super) 1.isinstance(obj,cls) 判断obj是不是由cls产生的类 2.issubclass(sub, ...
- 利用websocket实现微信二维码码扫码支付
由于业务需要引入微信扫码支付,故利用websocket来实现消息推送技术. 实现大致流程:首先客户端点击微信支付按钮,触发微信支付接口,同时微信支付响应成功参数后,连接websocket客户端,此刻利 ...
- Think twice before starting the adventure
杂文一篇. 1. 取名字真心是一件特别困难的事情.这位独立开发者花了将近两天的时间,给他的私人项目取了个名字:这篇博客<为何我不鸟你的开源项目>里显然还忽视了一个原因,就是名字取得太烂以至 ...
- Java工具eclipse控制台console输出乱码问题
捣鼓了一下午,终于tm解决! 我的是Scanner读入,println打印乱码问题. 首先在cmd窗口运行java,是没有乱码问题的,这证明了在cmd窗口时Scanner输入的和println打印的编 ...
- dell T130服务器加内存
需求:客户一台dell T130塔式服务器,由于本机只有一条8G内存,系统运行比较慢,需要再增加一条8G内存. 增加过程:第一次增加时由于没有注意机器上内存频率是2133的,所以新增加的一条2400频 ...
- kubernets基础
1.定义和功能. 1.1定义:kubernets解释为舵手或者飞行员,以Borg为主衍生出. 1.2功能:自动装箱,自我修复,水平扩展,服务发现和负载均衡,自动发布和回滚. 密钥和配置管理,存储编排, ...