<p>You are given a string <code>s</code> that consists of lowercase English letters.</p>

<p>A string is called <strong>special</strong> if it is made up of only a single character. For example, the string <code>&quot;abc&quot;</code> is not special, whereas the strings <code>&quot;ddd&quot;</code>, <code>&quot;zz&quot;</code>, and <code>&quot;f&quot;</code> are special.</p>

<p>Return <em>the length of the <strong>longest special substring</strong> of </em><code>s</code> <em>which occurs <strong>at least thrice</strong></em>, <em>or </em><code>-1</code><em> if no special substring occurs at least thrice</em>.</p>

<p>A <strong>substring</strong> is a contiguous <strong>non-empty</strong> sequence of characters within a string.</p>

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

<pre>
<strong>Input:</strong> s = &quot;aaaa&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> The longest special substring which occurs thrice is &quot;aa&quot;: substrings &quot;<u><strong>aa</strong></u>aa&quot;, &quot;a<u><strong>aa</strong></u>a&quot;, and &quot;aa<u><strong>aa</strong></u>&quot;.
It can be shown that the maximum length achievable is 2.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;abcdef&quot;
<strong>Output:</strong> -1
<strong>Explanation:</strong> There exists no special substring which occurs at least thrice. Hence return -1.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;abcaba&quot;
<strong>Output:</strong> 1
<strong>Explanation:</strong> The longest special substring which occurs thrice is &quot;a&quot;: substrings &quot;<u><strong>a</strong></u>bcaba&quot;, &quot;abc<u><strong>a</strong></u>ba&quot;, and &quot;abcab<u><strong>a</strong></u>&quot;.
It can be shown that the maximum length achievable is 1.
</pre>

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

<ul>
	<li><code>3 &lt;= s.length &lt;= 50</code></li>
	<li><code>s</code> consists of only lowercase English letters.</li>
</ul>

class Solution:
    def maximumLength(self, s: str) -> int:

        # "abadea" -> 1

        # "aaaa" -> 4 times -> 1 -> 2
        #  -> 2
        # magif_function ("aaaa"):

        # 3 
        # "aaaa baaaa", b, "aaaa"

        # a -> 2
        

        # 3, 4 
        # 4, 

        # "aa" b "aa" b "aa"
        # "aaaabdddddaaaaaddaaaa"
        
        # "ababa"
        #  a -> 1

        # "caaaa baaaa", b, "aaaa"

        # "aaa"

        # m = (1 + 3)//2 -> 2
        # r = 1
        # l = 1
        # mid = (1 + 1)// 2 = 1
        # l = 1 + 1 = 2
        # r = r

        # "aab"

        # l = 1, r =3
        # m = 2
        
        # l = 1
        # r = 1
        # m = 1

        # r = 0
        # l = 1


        def binary_search(l, r):
            res = -1
            while l <= r:
                mid = (l + r)//2

                if magic_function(mid):
                    res = mid
                    l = mid + 1
                else:
                    r = mid -1
            return res


        def magic_function(k):
            j = 0
            record = defaultdict(int)
            frequency = Counter(s[:k-1])
            j = 0
            for i in range(k-1,len(s)):
                frequency[s[i]] += 1
                if len(frequency) == 1:
                    record[s[i]] += 1
                    if record[s[i]] >= 3:
                        return True
                frequency[s[j]] -=1
                if frequency[s[j]] < 1:
                    del frequency[s[j]]
                j+=1
            
            return False
        
        # ans = 1
        # for i in range(1, len(s) - 1):
        #     if magic_function(i):
        #         ans = i
        
        # O nlogn binaty seac

        return binary_search(1, len(s))

            



            
                    



        


        
        
        



        
        
75