LC76 Minimum Window Substring

利用哈希表和滑动窗口来做题。一开始窗口内没有包含所有T的字符,扩大窗口直到包含T所有字符为止。然后再将窗口的左端向右移动,直到不能移动为止(再移动的话窗口内就没有所有T的字符了)。然后再移动窗口右端。如此循环。

 1 class Solution {
 2 public:
 3     string minWindow(string s, string t) {
 4         string str="";
 5         if(s==""||t==""||(s.size()<t.size()))
 6             return str;
 7         vector<int> expect(256,0);
 8         vector<int> real(256,0);
 9         for(int i=0;i<t.size();i++)
10             expect[t[i]]++;
11         int front=0;
12         int end=0;
13         int len=999999999;
14         int start=0;
15         for(;end<s.size();end++)
16         {
17             if(expect[s[end]]>0)
18             {
19                 real[s[end]]++;
20                 if(real[s[end]]<=expect[s[end]])
21                     count++;
22             }
23             if(count==t.size())
24             {
25                 while(expect[s[front]]==0||real[s[front]]>expect[s[front]])
26                 {
27                     real[s[front]]--;
28                     front++;
29                 }
30                 if(len>end-front+1)
31                 {
32                     len=end-front+1;
33                     start=front;
34                 }
35             }
36         }
37         return (999999999==len)?"":s.substr(start,len);
38     }
39 };

时间: 2024-07-29 10:05:17

LC76 Minimum Window Substring的相关文章

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层楼,每层楼里放有一个物品,现在老板给你一个物品清单,里面是要你集齐的物品,你可以乘坐电梯,但是电梯只停一次,停在哪一层,就从哪一层开始向楼上搜集物品,至于要在那层停电梯,由你自己选择. 这里我们当然选择即能集齐物品

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 &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;

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

【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