LeetCode 76. Minimum Window Substring / 567. Permutation in String
76. Minimum Window Substring
典型Sliding Window的问题,维护一个区间,当区间满足要求则进行比较选择较小的字串,重新修改start位置。
思路虽然不难,但是如何判断当前区间是否包含所有t中的字符是一个难点(t中字符有重复)。可以通过一个hashtable,记录每个字符需要的数量,这个数量可以为负(当区间内字符数超过所需的数量)。还需要一个count判断多少字符满足要求了,如果等于t.size(),说明当前窗口的字串包含t里所有的字符了。
class Solution { public:string minWindow(string s, string t) {unordered_map<char,int> hash; // char and the num it needs (it can be minus)int start=0;string res; int min_len=INT_MAX;for (char ch:t) hash[ch]++; int count=0;for (int end=0;end<s.size();++end){if (hash.count(s[end])){--hash[s[end]];if (hash[s[end]]>=0) ++count;while (count==t.size()){if (end-start+1<min_len){min_len = end-start+1;res = s.substr(start,end-start+1);}if (hash.count(s[start])){++hash[s[start]];if (hash[s[start]]>0) --count;}++start;}}}return res;} };
567. Permutation in String
和上面一题几乎一模一样,但是全排列的话,不能有其他的字母。这也很好解决,如果count==s1.size(),且字符串长度和s1.size()相等的话,就一定是s1的一个排列。
其实这道题和上一道题,都不用加 hash.count 判断当前字符在不在s1中,因为不在s1中的字符出现后,hash[ch]一定<0;从窗口移除后hash[ch]也不会超过0,因此对count没有影响。
这道题也和 438. Find All Anagrams in a String 很像,由于需要找的字符串长度是固定的,也可以固定长度,滑动窗口来做。
class Solution { public:bool checkInclusion(string s1, string a) {unordered_map<char,int> hash;for (char ch:s1) ++hash[ch];int count=0;int start=0;for (int end=0;end<a.size();++end){if (hash.count(a[end])){ //this char is in s1--hash[a[end]];if (hash[a[end]]>=0) ++count;while (count==s1.size()){if (end-start+1==s1.size()) return true;if (hash.count(a[start])){++hash[a[start]];if (hash[a[start]]>0) --count;}++start;}}}return false;} };
转载于:https://www.cnblogs.com/hankunyan/p/9603798.html
LeetCode 76. Minimum Window Substring / 567. Permutation in String相关推荐
- 【打印代码+好好理解+子串问题】LeetCode 76. Minimum Window Substring
LeetCode 76. Minimum Window Substring 字符串子串问题!!!理解这题真是花了很大的功夫!!! 参考链接:https://blog.csdn.net/weixin_4 ...
- 76. Minimum Window Substring
最后更新 一刷 08-Jan-2017 昨天Amazon group面结束,刚回家. 国内以前喜欢的女生结婚了,嘿嘿...好开心呀~~ 这次面试感觉自己的做法完爆别人,比什么2 greedy好多了= ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
- 【字符串全排列】LeetCode 567. Permutation in String
[字符串全排列]LeetCode 567. Permutation in String Solution1:[超时,未能AC] 得到s1的所有全排列组合,然后在s2中查找s1的全排列集合 因为超时,未 ...
- LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]...
题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...
- LeetCode - Minimum Window Substring
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- [LeetCode] Minimum Window Substring 散列映射问题
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- LeetCode 笔记系列16.1 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]...
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- LeetCode 笔记系列16.2 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]...
题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...
最新文章
- 360浏览器5兼容模式吗_Vue-cli项目,打包生产模式,部分用户360浏览器极速模式下奔溃...
- 零基础可以学好UI设计吗
- 理解统计信息(6/6):统计信息汇总贴
- 查看 Android SDK Build-tools 版本号
- VIP - virtual IP address
- Smokeping的参数使用说明
- Linux网络协议:当eBPF遇上Linux内核网络 | Linux内核之旅
- mysql的条件求和函数_mysql 带条件取count记录数,SUM()函数按条件求和
- redis的set类型
- IOS性能优化 - 分析应用
- pg数据库中读取json
- Ubuntu安装OpenCV4记录
- 什么是收缩压和舒张压?
- fbx模型压缩成gltf格式
- Git-删除文件后找回
- python制作有道翻译软件
- 数据驱动的网络入侵检测:最新动向与研究趋势
- 蒙氏教具 色板盒 颜色认知三段卡蒙氏教具
- 使用 FFmpeg 推流,使用 VLC 软件进行拉流
- Top 5 个功能强大免费开源的数据可视化平台