<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:
        # assume that a pair (c1,c2) will be the (max_char,min_char) pair
        # calculate the maximum variance for each k(k - 1) / 2 * 2 pairs. 
        # we multiply by 2 because (a,b) and (b,a) will be calculated as two distict possibilities

        # initial list of chars 'a-z'
        char_list = list(set(s))
        total_count = Counter(s)

        max_variance = 0

        def maxVarianceHelper(large, small):
            #print("called for ", large, small)
            # calculate the max_variance for the pair in O(n)
            # use Kadane's algorithm, imagine larger as +1 and smaller as -1
            # but the only modification is that you need at least one -1 in the 
            # substring
            start = 0
            count = {large: 0, small: 0}

            max_local_variance = 0
            num_small = total_count[small]

            # get the maximum diff
            for i, c in enumerate(s):
                if c in count:
                    count[c] += 1
                    if c in small: num_small -= 1
                    
                    if count[large] < count[small] and num_small > 0:
                        count[small] = count[large] = 0
                    if count[small]:
                        max_local_variance = max(max_local_variance, count[large] - count[small])
            
            return max_local_variance
                     
            
            #print("returning ", max_local_variance)
            return max_local_variance



        for i in range(len(char_list)):
            for j in range(i + 1,len(char_list)):
                # check the char pair for the two orders
                max_variance = max(max_variance, maxVarianceHelper(char_list[i], char_list[j]))
                max_variance = max(max_variance, maxVarianceHelper(char_list[j], char_list[i]))

        return max_variance 
2193