Leetcode Problem 2730. Find the Longest Semi-Repetitive Substring

2730. Find the Longest Semi-Repetitive Substring

Leetcode Solutions

Two Pointers Sliding Window Approach

  1. Initialize two pointers start and end to 0, and a variable max_length to 1 (since the minimum length of a semi-repetitive substring is 1).
  2. Initialize a variable consecutive_pairs to 0 to keep track of consecutive pairs of the same digit within the current window.
  3. Iterate through the string using the end pointer. a. If the current character is the same as the previous one, increment consecutive_pairs. b. While consecutive_pairs is greater than 1, move the start pointer to the right and decrement consecutive_pairs if we are breaking a consecutive pair. c. Update max_length with the maximum of its current value and the length of the current window (end - start + 1).
  4. Return max_length as the result.
UML Thumbnail

Simple O(n^) Brute Force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...