class Solution {
public:
string minWindow(string s, string t) {
unordered_map<char,int>need,window;
for(char c:t) need[c]++;
int left=0,right=0;
int valid=0;
int start=0,len=INT32_MAX;
while(right<s.size()){
char c=s[right];
right++;
if(need.count(c)){
window[c]++;
if(window[c]==need[c])
valid++;
}
while(valid==need.size()){
if(right-left<len){
start = left;
len = right-left;
}
char d = s[left];
left++;
if(need.count(d)){
if(window[d]==need[d])
valid--;
window[d]--;
}
}
}
return len == INT_MAX ?"" : s.substr(start, len);
}
};
- 字符串的排列
class Solution {
public:
bool checkInclusion(string s1, string s2) {
unordered_map<char,int>need,window;
for(char c:s1) need[c]++;
int left=0,right=0;
int valid=0;
int len = s1.length();
while(right<s2.size()){
char c = s2[right];
right++;
if(need.count(c)){
window[c]++;
if(window[c]==need[c])
valid++;
}
while(right-left>=len){
if(valid==need.size())
return true;
char d=s2[left];
left++;
if(need.count(d)){
if(window[d]==need[d])
valid--;
window[d]--;
}
}
}
return false;
}
};
- 无重复字符的最长子串
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int left=0,right=0,res=0;
unordered_map<char,int>window;
while(right<s.size()){
char c=s[right];
right++;
window[c]++;
while(window[c]>1){
char d = s[left];
left++;
window[d]--;
}
res=max(res,right-left);
}
return res;
}
};
- 替换后的最长重复字符
class Solution {
public:
int characterReplacement(string s, int k) {
unordered_map<char,int>window;
int left=0,right=0,res=0,maxCount=0;
while(right<s.size()){
window[s[right]]++;
maxCount = max(maxCount,window[s[right]]);
right++;
while(right-left-maxCount>k){
window[s[left]]--;
left++;
maxCount=0;
for(auto&t:window)
maxCount = max(maxCount,t.second);
}
res = max(res,right-left);
}
return res;
}
};
- 将 x 减到 0 的最小操作数
class Solution {
public:
int minOperations(vector<int>& nums, int x) {
int target = accumulate(nums.begin(),nums.end(),0)-x;
int res=-1,left=0,right=0, tempSum=0,n=nums.size();
while(right<n){
tempSum+=nums[right];
right++;
while(tempSum>target && left<right){
tempSum-=nums[left];
left++;
}
if(tempSum==target){
res =max(res,right-left);
}
}
return res==-1?-1:n-res;
}
};
- 最多 K 个重复元素的最长子数组
class Solution {
public:
int maxSubarrayLength(vector<int>& nums, int k) {
int left=0,right=0,n=nums.size();
unordered_map<int,int>window;
int res=0;
while(right<n){
int c=nums[right];
right++;
window[c]++;
while(window[c]>k){
int d=nums[left];
left++;
window[d]--;
}
res=max(res,right-left);
}
return res;
}
};
- 考试的最大困扰度
class Solution {
public:
int maxConsecutiveAnswers(string answerKey, int k) {
int left=0,right=0,n=answerKey.length();
int numT=0,numF=0,res=0;
while(right<n){
if(answerKey[right]=='T') numT++;
else numF++;
right++;
while(numT>k&&numF>k){
if(answerKey[left]=='T')
numT--;
else numF--;
left++;
}
res = max(res,right-left);
}
return res;
}
};