2024/10/19 灵茶题单 不定长滑动窗口
This commit is contained in:
parent
eeaac8389c
commit
5be658d1f4
|
@ -0,0 +1,31 @@
|
||||||
|
package com.cool.ling_cha_mount.sliding_windows;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @Author: Cool
|
||||||
|
* @Date: 2024 /10/19/19:07
|
||||||
|
* @Description: 2730. 找到最长的半重复子字符串
|
||||||
|
* Score 1502
|
||||||
|
*/
|
||||||
|
public class Num2730 {
|
||||||
|
public int longestSemiRepetitiveSubstring(String s) {
|
||||||
|
char[] chars = s.toCharArray();
|
||||||
|
int ans = 1, left = 0, same = 0, n = chars.length;
|
||||||
|
for (int right = 1; right < n; right++) {
|
||||||
|
if (chars[right] == chars[right - 1] && ++same > 1) {
|
||||||
|
for (left++; chars[left] != chars[left - 1]; left++) ;
|
||||||
|
same = 1;
|
||||||
|
}
|
||||||
|
ans = Math.max(ans, right - left + 1);
|
||||||
|
}
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
longestSemiRepetitiveSubstring("52233");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue