<p>You are given a digit string <code>s</code> that consists of digits from 0 to 9.</p>

<p>A string is called <strong>semi-repetitive</strong> if there is <strong>at most</strong> one adjacent pair of the same digit. For example, <code>&quot;0010&quot;</code>, <code>&quot;002020&quot;</code>, <code>&quot;0123&quot;</code>, <code>&quot;2002&quot;</code>, and <code>&quot;54944&quot;</code> are semi-repetitive while the following are not: <code>&quot;00101022&quot;</code> (adjacent same digit pairs are 00 and 22), and <code>&quot;1101234883&quot;</code> (adjacent same digit pairs are 11 and 88).</p>

<p>Return the length of the <strong>longest semi-repetitive <span data-keyword="substring-nonempty">substring</span></strong> of <code>s</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;52233&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">4</span></p>

<p><strong>Explanation:</strong></p>

<p>The longest semi-repetitive substring is &quot;5223&quot;. Picking the whole string &quot;52233&quot; has two adjacent same digit pairs 22 and 33, but at most one is allowed.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;5494&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">4</span></p>

<p><strong>Explanation:</strong></p>

<p><code>s</code> is a semi-repetitive string.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;1111111&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<p>The longest semi-repetitive substring is &quot;11&quot;. Picking the substring &quot;111&quot; has two adjacent same digit pairs, but at most one is allowed.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 50</code></li>
	<li><code>&#39;0&#39; &lt;= s[i] &lt;= &#39;9&#39;</code></li>
</ul>

class Solution:
    def longestSemiRepetitiveSubstring(self, s: str) -> int:
        # Maintain the longest semi repetitive substring 

        # we will start from the total length - 1 ( we find no of substrings satisfying)
        # If it doesnt satisfy we will shrink the windows to find length -2 pairs ... 

        # 52233         ---> length - 5 --> we will implement below logic 1st time 
        #               ---> contains 22, 33 semi repeating pairs ( count = 2 ) -> fail!!

        # lets shrink the windows by size 1 ---> 5 -1 = 4

        # Possible strings with length 4 will be as below  

        # 5223, 2233    ---> length - (5-1) = 4 ( we have implement below logic twice)

        # 5223 --> contains one semi repetitive - 22 --> we found it !!!!! return this
        # 2233 --> contains no repetitive - 22, 33 --> since we should have atmost 2 pairs

        # Lets see the logic
        # ==================
        
        r = 0  # Inializing the right pointer
        ip = 0 # This is inner pointer to count no of semi repetetive substrings.
        count_semi_repetitive = 0 #initializing semi repetitive valid strings

        windows_length = len(s)  #--> Intially  # 52233 [windows length - 4]

        while r + windows_length-1 <= len(s):
            current_string = s[r:r+windows_length]  # 5223 - 1st iteration 
                                                    # 2233 - 2nd iteration
            #Get the count of semi repetitive strins in current windows length
            while ip+1 < len(current_string):
                if current_string[ip:ip+2][0] == current_string[ip:ip+2][1]:
                    # 5223 
                    # 52,22,23
                    # count of semi repetitive = 1 ( which is 22 )
                    # This will check if 5 == 2 
                    #                    2 == 2 ( count be increased by 1)
                    #                    2 == 3 
                    count_semi_repetitive+=1
                ip+=1
            
            if count_semi_repetitive < 2: # for current string #5223 if count < 2 
                break # come out of the loop since we found longest semi repeating substring
            
            r += 1 # Increment the right pointer and start the window with same length 
            count_semi_repetitive = 0 # we will reset the count since sliding the window
            ip = 0 # we will reset the count of semi repeative pair since sliding the window

            # Resetting windows and length if greater than current length. 
            if r + windows_length - 1 > len(s): 
                windows_length-=1 # we will shrink the window.
                r = 0 # we will reset the right pointer to its inital position.

        return len(current_string) #Display the longest semi repeating substring
405