<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:
        # for evey alphabet, keep track of 3 longest special substrings

        queues = {
            letter: [] for letter in 'abcdefghijklmnopqrstuvwxyz'
        }

        n = len(s)

        start = 0
        i = 1
        while i < n:
            if s[i] != s[start]:
                # save alphabet streak
                length = i - start
                letter = s[start]

                # keep top 3 special substrings for current alphabet
                if len(queues[letter]) < 3:
                    heappush(queues[letter], (length, start))
                elif queues[letter][0][0] < length:
                    heappop(queues[letter])
                    heappush(queues[letter], (length, start))

                # reset alphabet
                start = i

            i += 1

        length = i - start
        letter = s[start]

        # keep top 3 special substrings for current alphabet
        if len(queues[letter]) < 3:
            heappush(queues[letter], (length, start))
        elif queues[letter][0][0] < length:
            heappop(queues[letter])
            heappush(queues[letter], (length, start))

        max_len = -1

        # for every alphabet, get retrieve and compare top 3
        for letter in 'abcdefghijklmnopqrstuvwxyz':
            lengths = []
            while len(queues[letter]) > 0:
                length, start = heappop(queues[letter])
                lengths.append(length)

            lengths.sort(reverse=True)

            if len(lengths) == 3 and lengths[0] == lengths[-1]:
                # maximum possible substrings, all at distinct locations
                max_len = max(max_len, lengths[0])
            elif len(lengths) >= 2:
                if lengths[0] == lengths[1] or lengths[0] == lengths[1]+1:
                    max_len = max(max_len, lengths[0]-1)
                else:
                    max_len = max(max_len, lengths[0]-2)
            elif len(lengths) == 1:
                max_len = max(max_len, lengths[0]-2)

        if max_len == 0:
            return -1
        return max_len

22300