Minimum Window Substring, 包含子串的最小窗口,双指针
问题描述:给定字符串S,子串T,求S中包含T的最小窗口
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"
.
算法分析:对于滑动窗口的题目,一般都是设置左右指针。这道题,首先用map1统计T中每个字符数,然后遍历S字符串,用map2统计T中字符在S中的个数,用count记录相同字符数,如果count==t.length说明当前窗口包含T,然后通过map2对比map1,来滑动窗口。
public class MinWindowSubstring
{
public String minWindow(String s, String t) {
if(t.length()>s.length())
return "";
String result = ""; //统计t中每个字符的个数
HashMap<Character, Integer> target = new HashMap<Character, Integer>();
for(int i=0; i<t.length(); i++)
{
char c = t.charAt(i);
if(target.containsKey(c))
{
target.put(c,target.get(c)+1);
}
else
{
target.put(c,1);
}
} //map统计t中字符在s中的个数
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int left = 0;//窗口左指针
int minLen = s.length()+1;//最小窗口 int count = 0; // 统计s中包含t中元素数 for(int i=0; i<s.length(); i++)
{
char c = s.charAt(i); if(target.containsKey(c))
{
if(map.containsKey(c))
{
if(map.get(c)<target.get(c))
{
count++;
}
map.put(c,map.get(c)+1);
}
else
{
map.put(c,1);
count++;
}
}//target.containsKey(c) if(count == t.length())//当前窗口包含所有t中元素
{
char sc = s.charAt(left);
//从左边开始滑动窗口
while (!map.containsKey(sc) || map.get(sc) > target.get(sc))
{
if (map.containsKey(sc) && map.get(sc) > target.get(sc))
{
map.put(sc, map.get(sc) - 1);
} left++; sc = s.charAt(left);
}
//计算最小窗口
if (i - left + 1 < minLen)
{
result = s.substring(left, i + 1);
minLen = i - left + 1;
}
}//count == t.length }//for return result;
}
}
Minimum Window Substring, 包含子串的最小窗口,双指针的更多相关文章
- 【LeetCode】76. Minimum Window Substring 最小覆盖子串(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址: https://leet ...
- 【LeetCode练习题】Minimum Window Substring
找出包含子串的最小窗口 Given a string S and a string T, find the minimum window in S which will contain all the ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
- leetcode76. Minimum Window Substring
leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...
- 【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 ...
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- 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 ...
- 76. Minimum Window Substring (JAVA)
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 53. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
随机推荐
- Android性能测试摘入(TestHome)
Android性能测试: 客户端性能测试 服务端性能测试 客户端性能测试: 1.ROM版本的性能测试(即手机的不同操作系统):关注功耗测试 2.应用的性能测 ...
- 修改jquery.automeplete,使其支持value匹配
原生只会去匹配label,可在实际使用中,可能需要匹配的值并不需要显示在label中,经过添加一个matchType属性解决 1.加入matchType选项,并默认为原生匹配 $.widget(&qu ...
- Hibernate 的查询
1. Hibernate 框架的查询方式 唯一标识OID的检索方式: session.get(对象.class, OID) 对象导航的方式; HQL 检索方式; QBC 检索方式; SQL 检索方式 ...
- selenium入门基础知识
内容转载自:http://blog.csdn.net/huangbowen521/article/details/7816538 1.selenium介绍: Selenium是一个浏览器自动化操作框架 ...
- filebeat 简介安装
Filebeat is a lightweight shipper for forwarding and centralizing log data. Installed as an agent on ...
- ngs中reads mapping-pku的生信课程
4.NGS中的reads mapping 顾名思义,就是将测序的得到的DNA定位在基因组上. 因为二代测序的得到的序列是较短的,reads mapping很好地解决了这个问题. 本质上reads ma ...
- likely(x)与unlikely(x) __builtin_expect
本文讲的likely()和unlikely()两个宏,在linux内核代码和一些应用中可常见到它们的身影.实质上,这两个宏是关于GCC编译器内置宏__builtin_expect的使用. 顾名思义,l ...
- Python:笔记(4)——高级特性
Python:笔记(4)——高级特性 切片 取一个list或tuple的部分元素是非常常见的操作.Python提供了切片操作符,来完成部分元素的选取 除了上例简单的下标范围取元素外,Python还支持 ...
- hive--udf函数(开发-4种加载方式)
UDF函数开发 标准函数(UDF):以一行数据中的一列或者多列数据作为参数然后返回解雇欧式一个值的函数,同样也可以返回一个复杂的对象,例如array,map,struct. 聚合函数(UDAF):接受 ...
- 理解音视频 PTS 和 DTS
视频 视频的播放过程可以简单理解为一帧一帧的画面按照时间顺序呈现出来的过程,就像在一个本子的每一页画上画,然后快速翻动的感觉. 但是在实际应用中,并不是每一帧都是完整的画面,因为如果每一帧画面都是完整 ...