[python]leetcode(76). Minimum Window Substring
来源:互联网 发布:linux换行 编辑:程序博客网 时间:2024/06/02 10:33
problem
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”.
solution
class Solution(object): def minWindow(self, s, t): from collections import defaultdict begin, end = 0, 0 head, length = 0, float('inf') counter = len(t) d = defaultdict(int) for i in t: d[i] += 1 while end < len(s): if d[s[end]] > 0: counter -= 1 d[s[end]] -= 1 end += 1 while counter == 0: if end - begin < length: head = begin length = end - begin if d[s[begin]] == 0: #只有在t中出现的字符才会等于零 counter += 1 d[s[begin]] += 1 begin += 1 return '' if length == float('inf') else s[head:head+length]
模板
当给定一个字符串让我们去找它的符合某些条件的子串时,通常使用哈希表和两个指针(滑动窗口),来解决问题。
int findSubstring(string s){ vector<int> map(128,0); int counter; // check whether the substring is valid int begin=0, end=0; //two pointers, one point to tail and one head int d; //the length of substring for() { /* initialize the hash map here */ } while(end<s.size()){ if(map[s[end++]]-- ?){ /* modify counter here */ } while(/* counter condition */){ /* update d here if finding minimum*/ //increase begin to make it invalid/valid again if(map[s[begin++]]++ ?){ /*modify counter here*/ } } /* update d here if finding maximum*/ } return d; }
阅读全文
0 0
- [python]leetcode(76). Minimum Window Substring
- [LeetCode] Minimum Window Substring -- python
- LeetCode 76 Minimum Window Substring
- LeetCode 76 Minimum Window Substring
- [leetcode 76] Minimum Window Substring
- [leetcode] 76 Minimum Window Substring
- LeetCode(76)Minimum Window Substring
- LeetCode 76 Minimum Window Substring
- leetcode || 76、Minimum Window Substring
- leetcode-76 Minimum Window Substring
- leetcode 76: Minimum Window Substring
- leetcode 76: Minimum Window Substring
- Leetcode#76||Minimum Window Substring
- leetcode 76 Minimum Window Substring
- leetcode 76:Minimum Window Substring
- Leetcode-76 Minimum Window Substring
- Leetcode 76 Minimum Window Substring
- LeetCode 76 Minimum Window Substring
- heartbeat与高可用
- 搜狐2014年产品经理笔试
- 有关qsort的应用以及冒泡排序的实现
- 第三周项目4(1)
- setuptools的安装
- [python]leetcode(76). Minimum Window Substring
- Django settings 中的STATIC_URL
- 排序算法(六)-归并排序
- configure: error: xml2-config not found. Please check your libxml2 installation
- 基于OpenCV的简易四肢位置识别与动作识别
- Min Stack 最小栈
- 八、JDBC——分页查询(mysql的limit方法)
- 高质量深度学习资源总结:128篇论文,21大领域
- bzoj 1878(莫队)(主席树)