2730. Find the Longest Semi-Repetitive Substring LeetCode Solution

In this guide, you will get 2730. Find the Longest Semi-Repetitive Substring LeetCode Solution with the best time and space complexity. The solution to Find the Longest Semi-Repetitive Substring problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find the Longest Semi-Repetitive Substring solution in C++
  4. Find the Longest Semi-Repetitive Substring solution in Java
  5. Find the Longest Semi-Repetitive Substring solution in Python
  6. Additional Resources
2730. Find the Longest Semi-Repetitive Substring LeetCode Solution image

Problem Statement of Find the Longest Semi-Repetitive Substring

You are given a digit string s that consists of digits from 0 to 9.
A string is called semi-repetitive if there is at most one adjacent pair of the same digit. For example, “0010”, “002020”, “0123”, “2002”, and “54944” are semi-repetitive while the following are not: “00101022” (adjacent same digit pairs are 00 and 22), and “1101234883” (adjacent same digit pairs are 11 and 88).
Return the length of the longest semi-repetitive substring of s.

Example 1:

Input: s = “52233”
Output: 4
Explanation:
The longest semi-repetitive substring is “5223”. Picking the whole string “52233” has two adjacent same digit pairs 22 and 33, but at most one is allowed.

Example 2:

Input: s = “5494”
Output: 4
Explanation:
s is a semi-repetitive string.

Example 3:

Input: s = “1111111”
Output: 2
Explanation:
The longest semi-repetitive substring is “11”. Picking the substring “111” has two adjacent same digit pairs, but at most one is allowed.

See also  953. Verifying an Alien Dictionary LeetCode Solution

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2730. Find the Longest Semi-Repetitive Substring LeetCode Solution in C++

class Solution {
 public:
  int longestSemiRepetitiveSubstring(string s) {
    int ans = 1;
    int prevStart = 0;
    int start = 0;

    for (int i = 1; i < s.length(); ++i) {
      if (s[i] == s[i - 1]) {
        if (prevStart > 0)
          start = prevStart;
        prevStart = i;
      }
      ans = max(ans, i - start + 1);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2730. Find the Longest Semi-Repetitive Substring LeetCode Solution in Java

class Solution {
  public int longestSemiRepetitiveSubstring(String s) {
    int ans = 1;
    int prevStart = 0;
    int start = 0;

    for (int i = 1; i < s.length(); ++i) {
      if (s.charAt(i) == s.charAt(i - 1)) {
        if (prevStart > 0)
          start = prevStart;
        prevStart = i;
      }
      ans = Math.max(ans, i - start + 1);
    }

    return ans;
  }
}
// code provided by PROGIEZ

2730. Find the Longest Semi-Repetitive Substring LeetCode Solution in Python

class Solution:
  def longestSemiRepetitiveSubstring(self, s: str) -> int:
    ans = 1
    prevStart = 0
    start = 0

    for i in range(1, len(s)):
      if s[i] == s[i - 1]:
        if prevStart > 0:
          start = prevStart
        prevStart = i
      ans = max(ans, i - start + 1)

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.