LeetCode 题解(101): Minimum Window Substring
来源:互联网 发布:巨杉数据库 创始人 编辑:程序博客网 时间:2024/06/11 00:27
题目:
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.
题解:
双指针保持count == t.length()的移动。
c++版:
class Solution {public:string minWindow(string s, string t) {if (s.length() < t.length())return "";int count = 0;bool found = false;int start = 0, end = 0;int distance = INT_MAX;string result ="";int needToFind[256] = { 0 }, hasFound[256] = {0};for (int i = 0; i < t.length(); i++)needToFind[t[i]]++;while (end < s.length()) {if (needToFind[s[end]] == 0) {end++;continue;}hasFound[s[end]]++;if (hasFound[s[end]] <= needToFind[s[end]])count++;if (count == t.length()) {while (needToFind[s[start]] == 0 || hasFound[s[start]] > needToFind[s[start]]) {if (hasFound[s[start]] > needToFind[s[start]])hasFound[s[start]]--;start++;}if (end - start + 1 < distance) {distance = end - start + 1;result = s.substr(start, distance);}}end++;}return result;}};
Java版:
public class Solution { public String minWindow(String s, String t) { if(s.length() < t.length()) return ""; int[] hasFound = new int[256]; int[] needToFind = new int[256]; for(int i = 0; i < t.length(); i++) { needToFind[t.charAt(i)]++; } int distance = Integer.MAX_VALUE; int start = 0, end = 0; int count = 0; String result = ""; while(end < s.length()) { if(needToFind[s.charAt(end)] == 0) { end++; continue; } hasFound[s.charAt(end)]++; if(hasFound[s.charAt(end)] <= needToFind[s.charAt(end)]) count++; if(count == t.length()) { while(needToFind[s.charAt(start)] == 0 || hasFound[s.charAt(start)] > needToFind[s.charAt(start)]) { if(hasFound[s.charAt(start)] > needToFind[s.charAt(start)]) hasFound[s.charAt(start)]--; start++; } if(end - start + 1 < distance) { distance = end - start + 1; result = s.substring(start, end + 1); } } end++; } return result; }}
Python版:
import sysclass Solution: # @param {string} s # @param {string} t # @return {string} def minWindow(self, s, t): needToFind, hasFound, start, end, count, result, distance = [0] * 256, [0] * 256, 0, 0, 0, "", sys.maxint for i in t: needToFind[ord(i)] += 1 while end < len(s): if needToFind[ord(s[end])] == 0: end += 1 continue hasFound[ord(s[end])] += 1 if hasFound[ord(s[end])] <= needToFind[ord(s[end])]: count += 1 if count == len(t): while needToFind[ord(s[start])] == 0 or hasFound[ord(s[start])] > needToFind[ord(s[start])]: if hasFound[ord(s[start])] > needToFind[ord(s[start])]: hasFound[ord(s[start])] -= 1 start += 1 if end - start + 1 < distance: distance = end - start + 1 result = s[start: end+1] end += 1 return result
0 0
- LeetCode 题解(101): Minimum Window Substring
- leetcode:Minimum Window Substring题解(Java)
- LeetCode题解:Minimum Window Substring
- LeetCode - Minimum Window Substring 题解
- LeetCode题解——Minimum Window Substring
- leetcode题解-76. Minimum Window Substring
- LeetCode题解–76. Minimum Window Substring
- LeetCode Algorithms 76. Minimum Window Substring 题解
- leetcode题解c++ | 76. Minimum Window Substring
- LeetCode No.76 Minimum Window Substring 题解
- LeetCode: Minimum Window Substring
- LeetCode Minimum Window Substring
- LeetCode : Minimum Window Substring
- [LeetCode] Minimum Window Substring
- [Leetcode] Minimum Window Substring
- LeetCode: Minimum Window Substring
- [LeetCode]Minimum Window Substring
- [leetcode]Minimum Window Substring
- [5]_技术那么多,我该如何选择
- QCustomPlot使用手册(三)
- display getSize()
- POJ 1469 COURSES (二分图最大匹配 匈牙利算法)
- 像在vim在中一样使用androidstudio
- LeetCode 题解(101): Minimum Window Substring
- Uva - 455 - Periodic Strings
- 6.8 Animation动画
- lua 中的 bit extend 库 (可能仅对非负数有效)
- APP Launchimage软件启动图片
- Codeforces Round #306 (Div. 2) (ABCE题解)
- UVA 10635 Prince and Princess 最长公共子序列(nlongn)
- 黑马程序员_多线程间通信
- EXCEL内的字符比较公式