2024/10/16 灵茶题单 不定长滑动窗口
This commit is contained in:
parent
41276c624f
commit
9c977b3197
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue