滑动窗口解决Substring Search Problem
2018-07-18 11:19:19
一、Minimum Window Substring
问题描述:

问题求解:
public String minWindow(String s, String t) {
String res = "";
if (t.length() > s.length()) return res;
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < t.length(); i++) {
map.put(t.charAt(i), map.getOrDefault(t.charAt(i), 0) + 1);
}
int begin, end, count;
begin = 0;
end = 0;
count = map.size();
int minLen = Integer.MAX_VALUE;
for (; end < s.length(); end++) {
char c = s.charAt(end);
if (map.containsKey(c)) {
map.put(c, map.get(c) - 1);
if (map.get(c) == 0) count--;
}
while (count == 0) {
if (end - begin + 1 < minLen) {
res = s.substring(begin, end);
minLen = end - begin + 1;
}
char temp = s.charAt(begin);
if (map.containsKey(temp)) {
if (map.get(temp) == 0) count++;
map.put(temp, map.get(temp) + 1);
}
begin++;
}
}
return res;
}
二、Longest Substring Without Repeating Characters
问题描述:

问题求解:
public int lengthOfLongestSubstring(String s) {
int[] map = new int[256];
int res = 0;
int begin = 0;
for (int end = 0; end < s.length(); end++) {
char c = s.charAt(end);
map[c]++;
while (map[c] > 1) {
map[s.charAt(begin)]--;
begin++;
}
res = Math.max(res, end - begin + 1);
}
return res;
}
三、Substring with Concatenation of All Words
问题描述:

问题求解:
本题其实是一道拓展题,这里题目中给出了所有单词的长度相同,也就意味着可以将所有的单词看作单个字母来进行处理,如果是这样的话,那么外层循环很显然就是word length了。之后通过两个Map对单词进行计数就可以进行解决。
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> res = new ArrayList<>();
if (s == null || s.length() == 0 || words.length == 0) return res;
Map<String, Integer> map = new HashMap<>();
int wl = words[0].length();
int num = words.length;
int n = s.length();
if (n < num * wl) return res;
for (int i = 0; i < num; i++)
map.put(words[i], map.getOrDefault(words[i], 0) + 1);
for (int i = 0; i < wl; i++) {
int begin, end, cnt;
Map<String, Integer> seen = new HashMap<>();
begin = i;
cnt = 0;
for (end = begin; end <= n - wl; end += wl) {
String str = s.substring(end, end + wl);
// 匹配成功
if (map.containsKey(str)) {
seen.put(str, seen.getOrDefault(str, 0) + 1);
cnt++;
// 若计数个数超过map, 则需要对窗口大小进行调整
while (seen.get(str) > map.get(str)) {
String temp = s.substring(begin, begin + wl);
seen.put(temp, seen.get(temp) - 1);
begin = begin + wl;
cnt--;
}
// 如果所有单词都匹配完成, 则向res中添加答案,同时将窗口大小向前挪动一步
if (cnt == num) {
res.add(begin);
String temp = s.substring(begin, begin + wl);
seen.put(temp, seen.get(temp) - 1);
begin = begin + wl;
cnt--;
}
}
// 当前匹配失败,则begin要从下一个开始,且所有的计数都要初始化
else {
seen.clear();
begin = end + wl;
cnt = 0;
}
}
}
return res;
}
四、Longest Substring with At Least K Repeating Characters
问题描述:

问题求解:
单纯的使用滑动窗口在修改窗口大小的时候会出现难以判断的情况,可以引入uniqueNum这个变量,来表征K个不同的字母,这样就可以大大降低问题的复杂度。
public int longestSubstring(String s, int k) {
int res = 0;
char[] chs = s.toCharArray();
for (int i = 1; i <= 26; i++) {
res = Math.max(res, helper(chs, k, i));
}
return res;
}
private int helper(char[] chs, int k, int uniqueNum) {
int res = 0;
int[] cnt = new int[256];
int begin = 0;
int nolessthank = 0;
int curUnique = 0;
for (int end = 0; end < chs.length; end++) {
if (cnt[chs[end]] == 0) curUnique++;
if (cnt[chs[end]] == k - 1) nolessthank++;
cnt[chs[end]]++;
while (curUnique > uniqueNum) {
if (cnt[chs[begin]] == k) nolessthank--;
if (cnt[chs[begin]] == 1) curUnique--;
cnt[chs[begin]]--;
begin++;
}
if (curUnique == uniqueNum && nolessthank == curUnique)
res = Math.max(res, end - begin + 1);
}
return res;
}
五、Max Consecutive Ones III
问题描述:

问题求解:
这种问题一般要么dp,要么滑动窗口,这种敏感性要有。
本题就是使用滑动窗口来进行解决的问题。实质上就是求解最长的substring其中至多包含k个0。
public int longestOnes(int[] A, int K) {
int res = 0;
int begin = 0;
int cnt = 0;
for (int end = 0; end < A.length; end++) {
if (A[end] == 0) cnt+= 1;
while (cnt > K) {
if (A[begin] == 0) cnt -= 1;
begin++;
}
res = Math.max(res, end - begin + 1);
}
return res;
}
滑动窗口解决Substring Search Problem的更多相关文章
- 滑动窗口-Substring Search Problem
2018-07-18 11:19:19 一.Minimum Window Substring 问题描述: 问题求解: public String minWindow(String s, String ...
- [LeetCode]438. 找到字符串中所有字母异位词、76. 最小覆盖子串(滑动窗口解决子串问题系列)
题目438. 找到字符串中所有字母异位词 给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引. 说明: 字母异位词指字母相同,但排列不同的字符 ...
- 滑动窗口解决最小子串问题 leetcode3. Longest Substring Without Repeating Characters
问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...
- 彻底搞通TCP滑动窗口
在我们当初学习网络编程的时候,都接触过TCP,在TCP中,对于数据传输有各种策略,比如滑动窗口.拥塞窗口机制,又比如慢启动.快速恢复.拥塞避免等.通过本文,我们将了解滑动窗口在TCP中是如何使用的. ...
- [Leetcode]双项队列解决滑动窗口最大值难题
这道题是从优先队列的难题里面找到的一个题目.可是解法并不是优先队列,而是双项队列deque 其实只要知道思路,这一道题直接写没有太大的问题.我们看看题 给定一个数组 nums,有一个大小为 k 的滑动 ...
- [Leetcode 3] 最长不重复子串 Longest substring without repeating 滑动窗口
[题目] Given a string, find the length of the longest substring without repeating characters. [举例] Exa ...
- POJ 3320 Jessica's Reading Problem (滑动窗口)
题意:给定一个序列,求一个最短区间,使得这个区间包含所有的种类数. 析:最近刚做了几个滑动窗口的题,这个很明显也是,肯定不能暴力啊,时间承受不了啊,所以 我们使用滑动窗口来解决,要算出所有的种数,我用 ...
- leetcode 3 Longest Substring Without Repeating Characters(滑动窗口)
用滑动窗口的思想来做.用一个unordered_map来查询之前的char有没有在现在的窗口中. class Solution { public: int lengthOfLongestSubstri ...
- LeetCode编程训练 - 滑动窗口(Sliding Window)
滑动窗口基础 滑动窗口常用来解决求字符串子串问题,借助map和计数器,其能在O(n)时间复杂度求子串问题.滑动窗口和双指针(Two pointers)有些类似,可以理解为往同一个方向走的双指针.常用滑 ...
随机推荐
- linux mysql安装问题
1.rpm -qa | grep mysql //首先检查是否安装了mysql 2.如果安装了,卸载 rpm -e mysql 3\ 下载地址 http://dev.mysql.com/d ...
- Rpgmakermv(5) MiniLabel插件介绍
============================================================================ Introduction ========== ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON max_connection
zw版[转发·台湾nvp系列Delphi例程]HALCON max_connection procedure TForm1.Button1Click(Sender: TObject);var ho_I ...
- LoadRunner11支持的浏览器小结-Loadrunner11打不开IE浏览器的问题
http://www.cnblogs.com/qmfsun/p/4807237.html
- 2018-2019-2 20165209 《网络对抗技术》Exp4:恶意代码分析
2018-2019-2 20165209 <网络对抗技术>Exp4:恶意代码分析 1 基础问题回答和实验内容 1.1基础问题回答 如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监 ...
- nginx 参考文章汇总
Nginx 反向代理.负载均衡.页面缓存.URL重写及读写分离详解: http://freeloda.blog.51cto.com/2033581/1288553 Nginx开发从入门到精通: htt ...
- clock_gettime接口和linux时间系统
RTC/OSCRTC,Real Time Clock,真实时钟,也叫做CMOS时钟,又叫walltimeOSC,Operation System Clock,系统时钟.在开机时操作系统取得RTC中的时 ...
- [转载] iframe嵌入网页的用法
iframe并不是很常用的,在标准的网页中非常少用.但是有朋友经常问到,下面我简单地介绍一下它的用法,你只要熟练掌握这些参数足矣. <iframe>也应该是框架的一种形式,它与<fr ...
- MySQL数据库----存储过程
存储过程 存储过程包含了一系列可执行的sql语句,存储过程存放于MySQL中,通过调用它的名字可以执行其内部的一堆sql -- 存储过程的优点: -- 1.程序与数据实现解耦 -- 2.减少网络传输的 ...
- camera按键采集图像及waitKey的用法
前言 项目需要通过摄像头采集图像并保存,主要是用于后续的摄像头标定.实现过程其实很简单,需要注意一些细节. 系统环境 系统版本:ubuntu16.04:opencv版本:opencv2.4.13:编程 ...