Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Let <code>len[i]</code> be the length of the longest special string ending with <code>s[i]</code>.
If <code>i > 0</code> and <code>s[i] == s[i - 1]</code>, <code>len[i] = len[i - 1] + 1</code>. Otherwise <code>len[i] == 1</code>.
Group all the <code>len[i]</code> by <code>s[i]</code>. We have at most <code>26</code> groups.
The maximum value of the third largest <code>len[i]</code> in each group is the answer.
We only need to maintain the top three values for each group. You can use sorting, heap, or brute-force comparison to find the third largest value in each group.