<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:
        # Kadane's Algorithm Leetcode Editorial
        # Must keep restminor otherwise test cases fail because we need to know how many minor are remaining
        # must create hashMap to get restMinor
        # pairs have (a,b) (b,a) repeating letters
        # TC - O(n * k**2) Where n = len(s) and k = distinct characters in s.
        # SC - O(1) # hashmap max O(26) = O(1) and remaining O(1) variables like majorCount etc
        
        hashMap = {}
        for ch in s:
            hashMap[ch] = 1 + hashMap.get(ch, 0)
            
        charList = list(hashMap.keys())
        count_char = len(charList)
        pairs = []  # note (a,b) and (b,a) are distinct pair because in 1st a is major in 2nd b is major
        for i in range(count_char):
            for j in range(count_char):
                if i != j:
                    pairs.append((charList[i], charList[j]))
                    
                    
        self.globalVar = 0
        def getLocalVar(major, minor, majorCount, minorCount, restMinor):
            for i in range(len(s)):
                if s[i] == major:
                    majorCount += 1
                elif s[i] == minor:
                    minorCount += 1
                    restMinor -= 1
                    
                localVar = majorCount - minorCount
                if localVar > 0 and minorCount > 0:
                    self.globalVar = max(self.globalVar, localVar)
                    
                if localVar < 0 and restMinor > 0:
                    # reset to zero, going ahead we need to find substring that has majorCount > minorCount
                    majorCount = 0
                    minorCount = 0
        
        for major, minor in pairs:
            getLocalVar(major, minor, 0 , 0, hashMap[minor])
        
        return self.globalVar
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        '''
        hashMap = {}
        for ch in s:
            hashMap[ch] = 1 + hashMap.get(ch, 0)
        charList = list(hashMap.keys())  # gives distinct characters list
        len_h = len(charList)    # k number of distinct charaters, max k can be 26
        pairs = []
        for i in range(len_h):   # TC of this for loop = O(k*k)
            for j in range(len_h):
                if i != j:
                    pairs.append((charList[i], charList[j]))   
                    # i major j minor, can't do charSet[i] so made a charList from cahrSet
                    
        #print(pairs)
        self.globalmax = 0
        def getLocalVar(major, minor, majorCount, minorCount, restminor):
            for i in range(len(s)):
                if s[i] == major:
                    majorCount += 1
                elif s[i] == minor:
                    minorCount += 1
                    restminor -= 1  #gives minor remaining in rest of the string

                localMax = majorCount - minorCount
                if localMax > 0 and minorCount > 0:
                    self.globalmax = max(self.globalmax, localMax)
            
                if localMax < 0 and restminor > 0:
                    minorCount = 0
                    majorCount = 0
                

        for major, minor in pairs:     # has k**2 pairs in total
            getLocalVar(major, minor, 0, 0, hashMap[minor]) # requires to go through n len of s thus total for loop takes O(n * k**2)
        return self.globalmax
        '''
5452