3. 无重复字符的最长子串
class Solution {
public:int lengthOfLongestSubstring(string s) {int n=s.length(),ans=0,left=0;unordered_map<char,int> w;for(int i=0;i<n;i++){char c=s[i];while(w[c])w.erase(s[left++]);w[c]++;ans=max(ans,i-left+1);}return ans;}
};
1493:删除一个元素以后全为1的最长子数组
class Solution {
public:int longestSubarray(vector<int>& nums) {int ans=0; int tag=0;int left=0;for(int i=0;i<nums.size();i++){if(nums[i]==0){if(tag){ while(nums[left])left++;tag--;left++;}tag++;}ans=max(ans,i-left);}return ans;}
};
2730. 找到最长的半重复子字符串
class Solution {
public:int longestSemiRepetitiveSubstring(string s) {int ans=1;int left=0;int n=s.size(); int num=0;for(int i=1;i<n;i++){if(s[i]==s[i-1])num++;if(num>1){for(left++;s[left]!=s[left-1];left++);num=1;}ans=max(ans,i-left+1);}return ans;}
};