Minimum Window Substring
来源:互联网 发布:淘宝大学vip课有效果吗 编辑:程序博客网 时间:2024/06/10 09:27
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”.
题目的要求是寻找最小覆盖子串,如果找不到返回”“。在解题过程中,我们可以利用hash的思想,用thash[T[i]]来存储,字符串T中各个单词出现的次数。
class Solution {public: string minWindow(string s, string t) { vector<int> shash(128,0); vector<int> thash(128,0); for(int i=0;i<t.size();i++){ thash[t[i]]++; } int beg=-1,end=s.size(),start=0,found=0; int min_len=s.size(); for(int i=0;i<s.size();i++){ shash[s[i]]++; if(shash[s[i]]<=thash[s[i]]){//通过found来判断是否找到了所有t中的单词 found++; } if(found==t.size()){ while(start<i&&shash[s[start]]>thash[s[start]]){ //过滤掉字符串s中不属于t的字符 shash[s[start]]--; //或者属于t中的字符,但不是坐标最大的那个,如bba,ba取第二个b start++; } if(i-start+1<=min_len){ beg=start; end=i; min_len=i-start+1; } shash[s[start]]--; start++; found--; } } if(beg==-1) return ""; else return s.substr(beg,end-beg+1); }};
1 0
- LeetCode: Minimum Window Substring
- LeetCode Minimum Window Substring
- LeetCode : Minimum Window Substring
- [LeetCode] Minimum Window Substring
- [Leetcode] Minimum Window Substring
- Minimum Window Substring
- LeetCode: Minimum Window Substring
- Minimum Window Substring
- [LeetCode]Minimum Window Substring
- Minimum Window Substring
- Minimum Window Substring
- [leetcode]Minimum Window Substring
- Minimum Window Substring
- Leetcode:Minimum Window Substring
- LeetCode-Minimum Window Substring
- [leetcode] Minimum Window Substring
- [leetcode] Minimum Window Substring
- leetcode Minimum Window Substring
- java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.Has
- svn 解决cleanup 失败
- WebRTC优秀资源链接汇总
- hadoop中map和reduce的数量设置问题
- 利用patchca生成验证码
- Minimum Window Substring
- CSU 1831 Found(ACM-ICPC Asia Thailand National On-Site Programming Contest 2015)
- Unity实现标准光照模型的Shader代码
- SymPy学习之Elementary
- SymPy学习之Geometry Module
- Java传递音频给PC端C#程序<一>
- Java温习——面向对象第三部分
- VPN篇(5.4) 01. SSL VPN
- JAVA日期类求年龄并求出大于指定年龄的学生。本人菜鸟,第一次发JAVA博客,也是第一次发博客!望大家交流指正!