2024/10/16 灵茶题单 不定长滑动窗口

This commit is contained in:
Cool 2024-10-16 17:06:41 +08:00
parent 41276c624f
commit 9c977b3197
1 changed files with 31 additions and 0 deletions

View File

@ -0,0 +1,31 @@
package com.cool.ling_cha_mount.sliding_windows;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/10/16/17:05
* @Description: 3090. 每个字符最多出现两次的最长子字符串
* Level 2
* Hard 1
* Score 1329
*/
public class Num3090 {
public int maximumLengthSubstring(String s) {
int res=0;
int[] arr=new int[26];
int left=0;
for(int i=0;i<s.length();i++){
if(++arr[s.charAt(i)-'a']>2){
while(left<i){
if(--arr[s.charAt(left++)-'a']==2){
break;
}
}
}
res=Math.max(res,i-left+1);
}
return res;
}
}