返回介绍

solution / 2700-2799 / 2730.Find the Longest Semi-Repetitive Substring / README_EN

发布于 2024-06-17 01:03:00 字数 4249 浏览 0 评论 0 收藏 0

2730. Find the Longest Semi-Repetitive Substring

中文文档

Description

You are given a 0-indexed string s that consists of digits from 0 to 9.

A string t is called a semi-repetitive if there is at most one consecutive pair of the same digits inside t. For example, 0010, 002020, 0123, 2002, and 54944 are semi-repetitive while 00101022, and 1101234883 are not.

Return _the length of the longest semi-repetitive substring inside_ s.

A substring is a contiguous non-empty sequence of characters within a string.

 

Example 1:

Input: s = "52233"
Output: 4
Explanation: The longest semi-repetitive substring is "5223", which starts at i = 0 and ends at j = 3. 

Example 2:

Input: s = "5494"
Output: 4
Explanation: s is a semi-reptitive string, so the answer is 4.

Example 3:

Input: s = "1111111"
Output: 2
Explanation: The longest semi-repetitive substring is "11", which starts at i = 0 and ends at j = 1.

 

Constraints:

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

Solutions

Solution 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文