<p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>

<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>

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

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

<pre>
<strong>Input:</strong> s = &quot;aababbb&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings &quot;a&quot;, &quot;aa&quot;, &quot;ab&quot;, &quot;abab&quot;, &quot;aababb&quot;, &quot;ba&quot;, &quot;b&quot;, &quot;bb&quot;, and &quot;bbb&quot;.
- Variance 1 for substrings &quot;aab&quot;, &quot;aba&quot;, &quot;abb&quot;, &quot;aabab&quot;, &quot;ababb&quot;, &quot;aababbb&quot;, and &quot;bab&quot;.
- Variance 2 for substrings &quot;aaba&quot;, &quot;ababbb&quot;, &quot;abbb&quot;, and &quot;babb&quot;.
- Variance 3 for substring &quot;babbb&quot;.
Since the largest possible variance is 3, we return it.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;abcde&quot;
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>

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

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li>
	<li><code>s</code> consists of lowercase English letters.</li>
</ul>


class Solution:
    def largestVariance(self, s: str) -> int:
        res, cntr, chars = 0, [0]*26, [ord(c)-97 for c in set(s)]
        diff, prep = [[inf]*26 for _ in range(26)], [[0]*26 for _ in range(26)]

        for i, c in enumerate(s):
            cntr[c := ord(c)-97] += 1

            if cntr[c] == 1:
                res = max(res, max(cntr)-1)
            for b in chars:
                if c == b:
                    continue

                # print("substr:", s[:i+1])
                # print("cnt[c] = cnt[", chr(c+97), "]:", cntr[c])
                # print("cnt[b] = cnt[", chr(b+97), "]:", cntr[b])
                # print("diff[", chr(b+97), "][", chr(c+97), "]:", diff[b][c])
                # print("prep[", chr(b+97), "][", chr(c+97), "]:", prep[b][c])
                # print("res = max(", res, ",", "cntr[", chr(c+97), "]", "- cntr[",  chr(b+97), "]", " - diff[", chr(c+97), "][",  chr(
                #     b+97), "] =", cntr[c]-cntr[b] - diff[c][b], ") = ", max(res, cntr[c]-cntr[b] - diff[c][b]))

                res = max(res, cntr[c]-cntr[b] - diff[c][b])

                diff[b][c] = min(diff[b][c], prep[b][c])
                prep[b][c] = min(cntr[b]-cntr[c], 0)
            #     print("diff[",  chr(b+97), "][",  chr(c+97), "]:", diff[b][c])
            #     print("prep[",  chr(b+97), "][",  chr(c+97), "]:", prep[b][c])
            #     print()
            # print()

        return res

616