标签:etc The lex while unique nta imu nim complex
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"
Note:
""
.class Solution {
public String minWindow(String s, String t) {
int sl = s.length();
int tl = t.length();
if(sl == 0 || tl == 0 || sl < tl){
return "";
}
String res = "";
int l = 0;
int r = 0;
int[] freq = new int[256];
int co = 0;
int len = s.length() + 1;
for(char c: t.toCharArray()){
freq[c]++;
}
while(r<s.length()){
//不存在的置为-1
freq[s.charAt(r)]--;
if(freq[s.charAt(r)] >= 0){
co ++;
}
// 当找到substring需要夹逼找到最短的字串
if(co == tl){
while(freq[s.charAt(l)]<0){
freq[s.charAt(l++)]++;
}
//找到了更新
if(r - l + 1 < len){
len = r - l + 1;
res = s.substring(l,r+1);
}
//破坏循环继续找下一个
freq[s.charAt(l)]++;
l++;
co--;
}
r++;
}
return res;
}
}
Leetcode.76 Minimum Window Substring (Java)
标签:etc The lex while unique nta imu nim complex
原文地址:https://www.cnblogs.com/xm08030623/p/13475945.html