<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>

# Approach 


# Insights 
# we could build a current_substring with a list or set that only contains at most 1 adjacent set of numbers

# as we add the next value while iterating through the entire string, we append to the current_substring if the entire thing has only 1 set of adjacents. If the next value brings the 
# count up to 2, then start the current substring from scratch at the current element index
# ex) 52233
# 5, 52, 522 (1 adj), 5223 (1 adj), 52233 (2 adj) we again with 33 (1 adj)

# when a current_substring is terminated we check if it's length is the longer than the longest we've seen so far. If so, we update it as the longest

# At the end we return the longest length we've seen overall 


# Edge cases:
# - all values are the same in the input 
# - strings length of 1 - just return 1
# - strings where there are no repeats - thus return the length of the entire string 

# Summarized approach
# We iterate through the overall string and build a substring int by int and only permitting one 1 adj overall (tracking this in a variable). If the count of adj pairs reaches 2, we start the substring over again from the previous element. We then take the max of the current overall max length and the current length of the terminated substring. We then return the overall max substring at the end. 


# examples:
# "4411794"

# "52233"
# 1-2,3-4

class Solution:
    def longestSemiRepetitiveSubstring(self, s: str) -> int:
        if len(s) == 1:
            return 1
        
        current_substr = ''
        current_repeat_count = 0
        first_repeat_indices = []
        overall_substr_max_len = 0

        for i in range(len(s)):
            current_val = s[i]

            if i == 0:
                current_substr += current_val
                continue
            
            if current_val != current_substr[-1]:
                current_substr += current_val   
            else:
                if current_repeat_count == 1:
                    # Update to current and previous because they are the same
                    new_current_substr = s[first_repeat_indices[-1]:i+1]
                    if len(current_substr) > overall_substr_max_len:
                        overall_substr_max_len = len(current_substr)
                    
                    current_substr = new_current_substr
                    first_repeat_indices = [i-1, i]
                else:
                    current_substr += current_val
                    current_repeat_count += 1
                    first_repeat_indices = [i-1, i]
                
        if len(current_substr) > overall_substr_max_len:
            overall_substr_max_len = len(current_substr)                

        return overall_substr_max_len
        
51