From 5be658d1f4a0bbbf3534615be424200f3db38bb4 Mon Sep 17 00:00:00 2001 From: Cool <747682928@qq.com> Date: Sat, 19 Oct 2024 19:23:03 +0800 Subject: [PATCH] =?UTF-8?q?2024/10/19=20=E7=81=B5=E8=8C=B6=E9=A2=98?= =?UTF-8?q?=E5=8D=95=20=E4=B8=8D=E5=AE=9A=E9=95=BF=E6=BB=91=E5=8A=A8?= =?UTF-8?q?=E7=AA=97=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sliding_windows/Num2730.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/main/java/com/cool/ling_cha_mount/sliding_windows/Num2730.java diff --git a/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num2730.java b/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num2730.java new file mode 100644 index 0000000..87b3342 --- /dev/null +++ b/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num2730.java @@ -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"); + } +}