1 Star 0 Fork 332

peaking / leetcode

forked from doocs / leetcode 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 4.40 KB
一键复制 编辑 原始数据 按行查看 历史
ylb 提交于 2024-02-21 08:52 . feat: add problem tags (#2361)

2730. 找到最长的半重复子字符串

English Version

题目描述

给你一个下标从 0 开始的字符串 s ,这个字符串只包含 0 到 9 的数字字符。

如果一个字符串 t 中至多有一对相邻字符是相等的,那么称这个字符串 t半重复的 。例如,00100020200123200254944 是半重复字符串,而 001010221101234883 不是。

请你返回 s 中最长 半重复 子字符串的长度。

一个 子字符串 是一个字符串中一段连续 非空 的字符。

 

示例 1:

输入:s = "52233"
输出:4
解释:最长半重复子字符串是 "5223" ,子字符串从 i = 0 开始,在 j = 3 处结束。

示例 2:

输入:s = "5494"
输出:4
解释:s 就是一个半重复字符串,所以答案为 4 。

示例 3:

输入:s = "1111111"
输出:2
解释:最长半重复子字符串是 "11" ,子字符串从 i = 0 开始,在 j = 1 处结束。

 

提示:

  • 1 <= s.length <= 50
  • '0' <= s[i] <= '9'

解法

方法一:双指针

我们用双指针维护一个区间 $s[j..i]$,使得区间内最多只有一个相邻字符相等。我们用 $cnt$ 记录区间内相邻字符相等的个数,如果 $cnt \gt 1$,那么我们就需要移动左指针 $j$,直到 $cnt \le 1$。每一次,我们更新答案为 $ans = \max(ans, i - j + 1)$。

时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。

class Solution:
    def longestSemiRepetitiveSubstring(self, s: str) -> int:
        n = len(s)
        ans = cnt = j = 0
        for i in range(n):
            if i and s[i] == s[i - 1]:
                cnt += 1
            while cnt > 1:
                if s[j] == s[j + 1]:
                    cnt -= 1
                j += 1
            ans = max(ans, i - j + 1)
        return ans
class Solution {
    public int longestSemiRepetitiveSubstring(String s) {
        int n = s.length();
        int ans = 0;
        for (int i = 0, j = 0, cnt = 0; i < n; ++i) {
            if (i > 0 && s.charAt(i) == s.charAt(i - 1)) {
                ++cnt;
            }
            while (cnt > 1) {
                if (s.charAt(j) == s.charAt(j + 1)) {
                    --cnt;
                }
                ++j;
            }
            ans = Math.max(ans, i - j + 1);
        }
        return ans;
    }
}
class Solution {
public:
    int longestSemiRepetitiveSubstring(string s) {
        int n = s.size();
        int ans = 0;
        for (int i = 0, j = 0, cnt = 0; i < n; ++i) {
            if (i && s[i] == s[i - 1]) {
                ++cnt;
            }
            while (cnt > 1) {
                if (s[j] == s[j + 1]) {
                    --cnt;
                }
                ++j;
            }
            ans = max(ans, i - j + 1);
        }
        return ans;
    }
};
func longestSemiRepetitiveSubstring(s string) (ans int) {
	n := len(s)
	for i, j, cnt := 0, 0, 0; i < n; i++ {
		if i > 0 && s[i] == s[i-1] {
			cnt++
		}
		for cnt > 1 {
			if s[j] == s[j+1] {
				cnt--
			}
			j++
		}
		ans = max(ans, i-j+1)
	}
	return
}
function longestSemiRepetitiveSubstring(s: string): number {
    const n = s.length;
    let ans = 0;
    for (let i = 0, j = 0, cnt = 0; i < n; ++i) {
        if (i > 0 && s[i] === s[i - 1]) {
            ++cnt;
        }
        while (cnt > 1) {
            if (s[j] === s[j + 1]) {
                --cnt;
            }
            ++j;
        }
        ans = Math.max(ans, i - j + 1);
    }
    return ans;
}
Java
1
https://gitee.com/peaklee1134/leetcode.git
git@gitee.com:peaklee1134/leetcode.git
peaklee1134
leetcode
leetcode
main

搜索帮助