076 Minimum Window Substring

076 Minimum Window Substring

这道题就是用一个dictionary来跟踪有多少还需要Match的字母以及当前需要match的次数。 再用一个queue 来记录哪些字幕被Match了以及他们被match的顺序

from collections import defaultdict
class Solution:
    # @param {string} s
    # @param {string} t
    # @return {string}
    def minWindow(self, s, t):
        dic = defaultdict(int)
        for c in list(t):
            dic[c] += 1
        length, tmp, ans = len(t), [], s + "X"
        for i in range(0, len(s)):
            c = s[i]
            if c in dic:
                tmp.append(i)
                dic[c] -= 1
                if dic[c] >= 0:
                    length -= 1
            if length == 0:
                while tmp != []:
                    k = s[tmp[0]]
                    if dic[k] < 0:
                        dic[k] += 1
                        tmp.pop(0)
                    else:
                        break
                if i - tmp[0] < len(ans):
                    ans = s[tmp[0]:i+1]
                k = s[tmp[0]]
                dic[k] += 1
                tmp.pop(0)
                length += 1
        if ans == s + "X":
            return ""
        return ans
时间: 2024-10-05 12:33:33

076 Minimum Window Substring的相关文章

Java for LeetCode 076 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". Note: If there is no such windo

LeetCode: Minimum Window Substring [076]

[题目] 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

Minimum Window Substring leetcode

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". Note:

【leetcode】Minimum Window Substring

问题: 给定两个字符串,S,T,返回S中包含T中所有字符的最短的字串,若不存在,则返回"".时间复杂度为O(n). 例如:S = "ADOBCODEBANC" T = "ABC" 返回BANC 生活场景: 把问题具体化现实化一点.有n层楼,每层楼里放有一个物品,现在老板给你一个物品清单,里面是要你集齐的物品,你可以乘坐电梯,但是电梯只停一次,停在哪一层,就从哪一层开始向楼上搜集物品,至于要在那层停电梯,由你自己选择. 这里我们当然选择即能集齐物品

Minimum Window Substring &amp;&amp;&amp; Longest Substring Without Repeating Characters 快慢指针,都不会退,用hashmap或者其他结构保证

1 public class Solution { 2 public static int lengthOfLongestSubstring(String s) { 3 4 char[] arr = s.toCharArray(); 5 int pre = 0; 6 7 HashMap<Character, Integer> map = new HashMap<Character, Integer>(); 8 9 for (int i = 0; i < arr.length;

【leetcode刷题笔记】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". Note:If there is no such window i

53. Minimum Window Substring

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". Note:

Minimum Window Substring leetcode java

题目: 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 w

[leetcode]Minimum Window Substring @ Python

原题地址:https://oj.leetcode.com/problems/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" M