<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;= 5 * 10<sup>5</sup></code></li>
	<li><code>s</code> consists of only lowercase English letters.</li>
</ul>

class Solution:
    def maximumLength(self, s: str) -> int:
        # https://www.youtube.com/watch?v=HsflxahMwsI
        # the idea is to create a hashmap of conti chars and their counts
        # the max ans you can have is len(s) - 2, so once oyu have the chars/cnt map
        # do binary search from 1 to len(s)-2 and see if its possible to come up with
        # strs of len i that occurs 3 times

        # this function checks possibility of str of len = mid 3 times
        def isPossible(mid, hm):
            # mid here is the len of special substr we are tryign to create
            cnt = 0
            # for a conti set of chars, the no of substrs you can make of len mid = val-mid+1
            # val is the value in hm, if the no of substrs >= 3 that means it occurs atleast 3 
            # times and we can return true
            for key, values in hm.items():
                cnt = 0
                for val in values:
                    cnt += max(0, val - mid +1)
                if cnt >=3:
                    return True

            return False 
        
        # create a hm of continuous chars to cnt
        hm = defaultdict(list)
        last_char, cnt = s[0], 1
        for i in range(1, len(s)):
            if s[i] == last_char:
                cnt += 1
            else:
                hm[last_char].append(cnt) # it would look somehting like {a : [1, 1, 1], b = [1,1], c:[1]} for abcaba
                # reset vars
                last_char = s[i]
                cnt = 1
        
        hm[last_char].append(cnt)
        
        # do binary search to find the max possible len of strs we can do
        i, j = 1, len(s)-2
        op = -1 

        while i <= j:
            mid = (i+j)//2
            if isPossible(mid, hm):
                op =  mid
                # because atleast this much is possible, need to check if higher possible
                i = mid+1
            else:
                j = mid - 1
        
        return op
1376