From 247df5fb717648a575fc10f2d20c038336f93aca Mon Sep 17 00:00:00 2001 From: mbinary Date: Sun, 26 May 2019 21:43:30 +0800 Subject: [PATCH] Add min-window-substring using sliding window --- dataStructure/insert_remove_getRandom.py | 58 +++++++++++++++++++++++ string/min-window-substring.py | 60 ++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 dataStructure/insert_remove_getRandom.py create mode 100644 string/min-window-substring.py diff --git a/dataStructure/insert_remove_getRandom.py b/dataStructure/insert_remove_getRandom.py new file mode 100644 index 0000000..3beeacd --- /dev/null +++ b/dataStructure/insert_remove_getRandom.py @@ -0,0 +1,58 @@ +#coding: utf-8 +''' mbinary +####################################################################### +# File : insert_remove_getRandom.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.xyz +# Github: https://github.com/mbinary +# Created Time: 2019-05-24 10:01 +# Description: + insert, remove, getRandom 的摊还时间为 O(1), + 且有重复数据, remove 一次删除一个元素 +####################################################################### +''' + +class RandomizedCollection: + def __init__(self): + + self.vals=[] + self.index={} + + def insert(self, val: int) -> bool: + self.vals.append(val) + if val in self.index: + self.index[val].add(len(self.vals)-1) + return False + else: + self.index[val] = {len(self.vals)-1} + return True + def removeAll(self, val: int) -> bool: + if val not in self.index: + return False + begin = end = len(self.vals)-len(self.index[val]) + for idx in self.index.pop(val): + if idx int: + if self.vals: + return self.vals[random.randint(0,len(self.vals)-1)] diff --git a/string/min-window-substring.py b/string/min-window-substring.py new file mode 100644 index 0000000..fb834fe --- /dev/null +++ b/string/min-window-substring.py @@ -0,0 +1,60 @@ +#coding: utf-8 +''' mbinary +####################################################################### +# File : min-window-substring.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.xyz +# Github: https://github.com/mbinary +# Created Time: 2019-05-26 21:39 +# Description: +from leetcode-cn #76: https://leetcode-cn.com/problems/minimum-window-substring/ + + 给定一个字符串 S 和一个字符串 T,请在 S 中找出包含 T 所有字母的最小子串。 +输入: S = "ADOBECODEBANC", T = "ABC" +输出: "BANC" +说明: + 如果 S 中不存这样的子串,则返回空字符串 ""。 + 如果 S 中存在这样的子串,我们保证它是唯一的答案。 + +since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is also called Sliding Window Approach. + +L ------------------------ R , Suppose this is the window that contains all characters of T + L----------------- R , this is the contracted window. We found a smaller window that still contains all the characters in T + +When the window is no longer valid, start expanding again using the right pointer. +####################################################################### +''' + +from collections import defaultdict +class Solution: + def minWindow(self, s: str, t: str) -> str: + def expand(j,lacked,dic): + while jj-i+1: + ans = s[i-1:j] + return ans