GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — LeetCode/076 Minimum Window Substring.py at master · algorhythms/LeetCode Expand file tree/ Copy path076 Minimum Window Substring.pyMore file actions 64 lines (50 loc) · 1.92 KB / Copy path076 Minimum Window Substring.pyFile metadata and controls64 lines (50 loc) · 1.92 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 """ 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". Note: If there is no such window in S that covers all characters in T, return the emtpy string "". If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S. """ import sys __author__ = 'Danyang' class Solution(object): def minWindow(self, S, T): """ Algorithm: two pointers Aggressively enclose the chars until find all T, and then shrink the window as far as possible :param S: str :param T: str :return: str """ min_win = [0, sys.maxint] # [start, end) w_cnt = [0 for _ in range(256)] # window t_cnt = [0 for _ in range(256)] # 256 ascii, static for char in T: t_cnt[ord(char)] += 1 appeared_cnt = 0 lo = 0 for hi in xrange(1, len(S)+1): # expand val = S[hi-1] if t_cnt[ord(val)] > 0: w_cnt[ord(val)] += 1 if t_cnt[ord(val)] > 0 and w_cnt[ord(val)] <= t_cnt[ord(val)]: appeared_cnt += 1 # cache, determine when to decrease appeared_cnt # shrink if appeared_cnt == len(T): # until find all while w_cnt[ord(S[lo])] > t_cnt[ord(S[lo])] or t_cnt[ord(S[lo])] == 0: if w_cnt[ord(S[lo])] > 0: w_cnt[ord(S[lo])] -= 1 lo += 1 if min_win[1]-min_win[0] > hi-lo: min_win[0], min_win[1] = lo, hi if min_win[1] == sys.maxint: return "" else: return S[min_win[0]:min_win[1]] if __name__ == "__main__": assert Solution().minWindow("ADOBECODEBANC", "ABC") == "BANC" |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |