Java for LeetCode 076 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.
解题思路:
涉及到查找操作,将T放进tMap中,同时创建一个保存T中所有字符(不重复)的图sMap,用一个计数器表示sMap和tMap的重合程度,一旦sMap包含了tMap即找到了一组解,通过收缩begin指针获取刚好包含tMap的位置,之后就是比较大小了,JAVA实现如下:
public String minWindow(String s, String t) {
HashMap<Character, Integer> tMap = new HashMap<Character, Integer>();
HashMap<Character, Integer> sMap = new HashMap<Character, Integer>();
for (int i=0;i<t.length();i++){
sMap.put(t.charAt(i), 0);
if (!tMap.containsKey(t.charAt(i)))
tMap.put(t.charAt(i), 1);
else
tMap.put(t.charAt(i), tMap.get(t.charAt(i)) + 1);
}
int begin=0,count=0,minBegin=0,length=s.length()+1;;
for(int i=0;i<s.length();i++){
if(!tMap.containsKey(s.charAt(i)))
continue;
sMap.put(s.charAt(i), sMap.get(s.charAt(i))+1);
if(sMap.get(s.charAt(i))<=tMap.get(s.charAt(i)))
count++;
if(count==t.length()){
for(int j=begin;j<=i;j++){
if(!tMap.containsKey(s.charAt(j)))
continue;
if(sMap.get(s.charAt(j))>tMap.get(s.charAt(j))){
sMap.put(s.charAt(j),sMap.get(s.charAt(j))-1);
continue;
}
sMap.put(s.charAt(j),sMap.get(s.charAt(j))-1);
count--;
begin=j+1;
if(length>i-j){
length=i-j;
minBegin=j;
}
break;
}
}
}
return length!=s.length()+1?s.substring(minBegin, minBegin+length+1):"";
}
Java for LeetCode 076 Minimum Window Substring的更多相关文章
- [LeetCode] 76. Minimum Window Substring 解题思路
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [LeetCode] 76. Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [leetcode]76. 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 (hard) ★
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 076 Minimum Window Substring 最小窗口子字符串
给定一个字符串 S 和一个字符串 T,找到 S 中的最小窗口,它将包含复杂度为 O(n) 的 T 中的所有字符.示例:S = "ADOBECODEBANC"T = "AB ...
- Leetcode#76 Minimum Window Substring
原题地址 用两个指针分别记录窗口的左右边界,移动指针时忽略那些出现在S种但是没有出现在T中的字符 1. 扩展窗口.向右移动右指针,当窗口内的字符即将多于T内的字符时,停止右移 2. 收缩窗口.向右调整 ...
- [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
- 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 ...
随机推荐
- hdu 3068 最长回文子串 马拉车模板
前几天用后缀数组写过一次这题,毫无疑问很感人的TLE了-_-|| 今天偶然发现了马拉车模板,O(N)时间就搞定 reference:http://acm.uestc.edu.cn/bbs/read.p ...
- hdu 3068 最长回文子串 TLE
后缀数组+RMQ是O(nlogn)的,会TLE..... 标准解法好像是马拉车,O(n).... #include "algorithm" #include "cstdi ...
- map 几种遍历方法
public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...
- Integer.valueOf(String) 方法之惑
本文由 ImportNew - 靳禹 翻译自 stackoverflow.欢迎加入翻译小组.转载请见文末要求. 有个仁兄在 StackOverflow 上发起了一个问题,是这么问的: “ 我被下面的代 ...
- Hibernate检索策略(抓取策略)(Hibernate检索优化)
一.查询方法中get方法采用策略是立即检索,而load方法采用策略是延迟检索,延迟检索是在使用数据时才发送SQL语句加载数据 获取延迟加载数据方式:1.使用的时候,如果Customer c=sessi ...
- Protocol Buffer技术详解(Java实例)
Protocol Buffer技术详解(Java实例) 该篇Blog和上一篇(C++实例)基本相同,只是面向于我们团队中的Java工程师,毕竟我们项目的前端部分是基于Android开发的,而且我们研发 ...
- winscp私钥如何生成
问题1,我用SecureCRT 5.0的自带工具生成了密钥和公钥(分别是不带后缀名的密钥文件和.pub的公钥文件),传上服务器也可以正常使用. 但是我用其它一些客户端工具连接时需要的密钥文件是 ...
- 我的电脑右下角的日期也不见了只剩下时间,Win7系统,请问是什么原因啊?
A:今天是2013/10/10,日期变为八位,宽度就不够了,把任务栏拉宽就好了 Q:win7 任务栏时间区可以拉宽吗?使之显示日期等 A:你的任务栏锁定了的,右键单击任务栏-锁定任务栏前面的勾去掉,鼠 ...
- L18 如何快速查找文档获得帮助
原地址:http://www.howzhi.com/course/286/lesson/2121 查找文档快速 苹果提供了丰富的文档,以帮助您成功构建和部署你的应用程序,包括示例代码,常见问题解答,技 ...
- select function in ruby
http://ruby-doc.org/ http://ruby-doc.org/core-2.3.0/Array.html#method-i-select [1,2,3,4,5].select { ...