<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:
        from collections import Counter
        
        # Count the frequency of each character in the string
        counter = Counter(s)
        global_max = 0
        visit = set()
        
        for i in range(26):
            for j in range(26):
                # major and minor cannot be the same, and both must appear in s.
                if i == j or counter[chr(i + ord('a'))] == 0 or counter[chr(j + ord('a'))] == 0 or (i,j) in visit:
                    continue
                
                # Find the maximum variance of major - minor.
                major = chr(i + ord('a'))
                minor = chr(j + ord('a'))
                major_count = 0
                minor_count = 0
                
                # The remaining minor in the rest of s.
                rest_minor = counter[minor]
                
                for ch in s:
                    if ch == major:
                        major_count += 1
                    if ch == minor:
                        minor_count += 1
                        rest_minor -= 1
                    
                    # Only update the variance (local_max) if there is at least one minor.
                    if minor_count > 0:
                        global_max = max(global_max, major_count - minor_count)
                    
                    # We can discard the previous string if there is at least one remaining minor.
                    if major_count < minor_count and rest_minor > 0:
                        major_count = 0
                        minor_count = 0

                    visit.add((i,j))
        
        return global_max
5873