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 ...
随机推荐
- JS~json日期格式化
起因 对于从C#返回的日期字段,当进行JSON序列化后,在前台JS里显示的并不是真正的日期,这让我们感觉很不爽,我们不可能为了这东西,把所有日期字段都变成string吧,所以,找了一个JS的扩展方法, ...
- Yii2登陆添加验证码
models中 LoginForm.php public $verifyCode; public function rules() { return [ …… ['verifyCode', 'capt ...
- Bringing up interface eth0: Error:Connection activation failed:Device not managed by NetworkManager
Just follow the below steps and everything will be ok... 1. Remove Network Manager from startup Se ...
- javaScript基础练习题-下拉框制作(CSS)
http://www.imooc.com/video/155 慕课网的视频,直接上代码 <!DOCTYPE hmtl> <html> <head> <meta ...
- POJ2299 Ultra-QuickSort
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- MVC模式的学生信息增删改查
准备:建一个名为 userdb的数据库.建一个student表,有stuid,stuname,gender三个字段.其中stuid为主键.j加入相应的驱动包,相应的JSTL标签 先看目录结构 代码: ...
- json 数据交换格式与java
http://wiki.mbalib.com/wiki/数据交换 数据交换是指为了满足不同信息系统之间数据资源的共享需要,依据一定的原则,采取相应的技术,实现不同信息系统之间数据资源共享的过程. 数据 ...
- jquery发送ajax请求返回数据格式
jquery向服务器发送一个ajax请求后,可以返回多种类型的数据格式,包括:html,xml,json,text等. 1.html格式的数据 "<div class='comment ...
- 【wget】一条命令轻松备份博客(包括图片)
h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...
- C#编程总结 dynamic(转)
介绍 Visual C# 2010 引入了一个新类型 dynamic. 该类型是一种静态类型,但类型为 dynamic 的对象会跳过静态类型检查. 大多数情况下,该对象就像具有类型 object 一样 ...