LeetCode – Refresh – Minimum Window Substring

Notes:

1. When check left shifting, do not use continue, but break it!!! Otherwise, you wil fall into infinite loop

2. Initialize the map with T, not S!!!!

 1 class Solution {
 2 public:
 3     string minWindow(string S, string T) {
 4         int len = S.size(), count = 0, length = INT_MAX;
 5         string result;
 6         if (len == 0 || len < T.size()) return "";
 7         unordered_map<char, int> dict;
 8         for (char c : T) {
 9             if (dict[c]) dict[c]++;
10             else dict[c] = 1;
11         }
12         for (int left = 0, right = 0; right < len; right++) {
13             if (dict.find(S[right]) != dict.end()) {
14                 dict[S[right]]--;
15                 if (dict[S[right]] == 0) {
16                     count++;
17                 }
18             }
19
20             while (left < len) {
21                 if (dict.find(S[left]) != dict.end() && dict[S[left]] >= 0) break;
22                 if (dict.find(S[left]) != dict.end()) {
23                     dict[S[left]]++;
24                 }
25                 left++;
26             }
27
28             if (count == dict.size() && length > (right - left + 1)) {
29                 result = S.substr(left, right-left+1);
30                 length = right - left + 1;
31             }
32         }
33         return result;
34     }
35 };
时间: 2024-10-26 10:16:21

LeetCode – Refresh – Minimum Window Substring的相关文章

【leetcode】Minimum Window Substring

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

Leetcode 76 Minimum Window Substring. (最小窗口子字符串) (滑动窗口, 双指针)

目录 问题描述 例子 解决方案 ** Leetcode 76 ** 问题描述 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). 例子 Example: Input: S = "ADOBECODEBANC", T = "ABC" Output: "BANC&qu

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][JAVA] 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

leetCode 76.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(55):Minimum Window Substring(limits.h头文件)

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 (hard) ★

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

leetcode[76] Minimum Window Substring

给定两个串,S和T,在S中找到包含T的最短子串,如果不能包含,返回空字符. 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&

leetcode 76 Minimum Window Substring ----- 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 window i