<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:
        maxVariance = 0
        charCount = {}
        for i in range(len(s)):
            charCount[s[i]] = charCount.get(s[i], 0) + 1
        
        record = {}
        for char1 in charCount.keys():
            chars = set(charCount.keys())
            chars.remove(char1)
            for char2 in chars:
                record[(char1, char2)] = -math.inf # could store more data if needed
        # print(record)
        for char1, char2 in record.keys():
            currSum = 0
            maxSum = -math.inf
            startIndex = 0
            seen1 = seen2 = False
            for i in range(len(s)):
                currChar = s[i]
                if currChar == char1:
                    if currSum == -1:
                        currSum = 1
                        startIndex = i
                        seen1 = True
                        seen2 = False
                    else:
                        currSum += 1
                        seen1 = True
                elif currChar == char2:
                    if currSum == -1:
                        currSum = -1
                        startIndex = i
                        seen2 = True
                        seen1 = False
                    else:
                        currSum -= 1
                        seen2 = True
                
                if seen1 and seen2:
                    maxSum = max(maxSum, currSum)
            if seen2 == False and startIndex != 0 and s[startIndex - 1] == char2:
                currSum -= 1
                maxSum = max(maxSum, currSum)
            record[(char1, char2)] = maxSum
        print(record)
        for _, maxSum in record.items():
            maxVariance = max(maxVariance, maxSum)

        return maxVariance
        
4611