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

# 首先我们先找出以第i个element为结束点的最长的special string
# 然后我们对每个字母分类
# 比如说结束点为a的special string，可能是"a", "aaa", "aaaa"
# 注意：如果是“abaababaaa" 那么对于a来说，应该是a, a, aa，a, aa, aaa 6个
# 然后对于每个字母而言，我们找出第三长的。
# 为什么是第三长的？
# 因为最长的只会出现在自己的这个substring里面一次。第二长的除了出现在自己的substring，还会出现在最长的substring
# 比如 "abaaa" 就是 a a aa aaa, 那么就是 a

class Solution:
    def maximumLength(self, s: str) -> int:
        # find the longest substr ends at i
        longest_str = [1]
        for i in range(1, len(s), 1):
            if s[i] != s[i-1]:
                longest_str.append(1)
            else:
                longest_str.append(longest_str[i-1]+1)
        
        # creat a hashmap to store the substring length for each char
        hashmap = [[] for _ in range(26)]
        for i in range(len(s)):
            length = longest_str[i]
            hashmap[ord(s[i])-ord('a')].append(length)

        maximum_length = -1
        for lengthes in hashmap:
            if len(lengthes) < 3:
                continue
            else:
                lengthes.sort()
                maximum_length = max(maximum_length, lengthes[-3])
        return maximum_length

42600