滑动窗口解决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)有些类似,可以理解为往同一个方向走的双指针.常用滑 ...
随机推荐
- 教你玩转产品管理系统iClap(PC端功能篇)
之前和大家介绍了iClap的基础功能, 这一次针对PC端右侧的工具栏再做一个详细的介绍 随着版本的更新迭代,陆续会有更多工具和功能推出! 导航 为项目成员提供网址浏览访问导航服务,帮助项目成员快速查找 ...
- Git 常用的命令
基本内容: 工作区:就是你在电脑里能看到的目录. 暂存区:英文叫stage, 或index.一般存放在"git目录"下的index文件(.git/index)中,所以我们把暂存区有 ...
- EntityFramework包含作用
System.Data.Entity.Infrastructure.DbQuery的引用需要加入上面那个包
- HTML ajax 上传文件限制文件的类型和文件大小
html <input type="file" name="excel" id="excel_input" accept=&qu ...
- Python实现Json结构对比的小工具兼谈编程求解问题
摘要: 通过使用Python编写一个解析Json结构对比的小工具,来提炼编程求解的通用步骤和技巧. 难度: 初级 先上代码. jsondiff.py #!/usr/bin/python #_*_enc ...
- python-安装,设置环境变量(win10)
python官网: https://www.python.org/ 选择需要的版本下载 下载后安装 我装的是默认位置C:\Python27 打开环境变量设置: 右键电脑--->属性----> ...
- Java EE业务处理流程与XML的引入
Java EE基于MVC架构的业务处理流程 MVC架构业务处理流程 XML定义 XML是可扩展标记语言,标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言.XML被设计用于数据的存 ...
- 20165207 Exp4 恶意代码分析
目录 1.实验内容 1.1.系统运行监控 1.1.1.使用命令行创建计划任务 1.1.2.使用命令行借助批处理文件创建计划任务 1.1.3.分析netstat计划任务的最终结果 1.1.4.安装配置s ...
- 微信公众号为什么要加粉?流量,广告,KPI,吸粉,增粉
微信公众号为什么要加粉?流量,广告,KPI,吸粉,增粉 1.曾有人这样比喻:当你的粉丝超过100人时,你就像是一本内刊:超过1000人,你就像个布告栏:超过1万人,你就好比一本杂志:超过10万人,你就 ...
- python3.4学习笔记(十五) 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
python3.4学习笔记(十五) 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) python print 不换行(在后面加上,end=''),prin ...