<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:
        # freq = Counter(s)
        freq = [0] * 26
        for c in s:
            freq[ord(c) - ord('a')] += 1
        mx = 0
        for i in range(26):
            for j in range(26):
                if i==j or freq[i] == 0 or freq[j] == 0:
                    continue
                major = chr(i + 97)
                minor = chr(j + 97)
                mjc = 0
                mnc = 0
                rest = freq[j]
                for k in s:
                    if k == major:
                        mjc += 1
                    if k == minor:
                        mnc += 1
                        rest -= 1

                    if mnc > 0:
                        mx = max(mx, mjc - mnc)
                    if mnc > mjc and rest > 0:
                        mnc = 0
                        mjc = 0
        return mx

### Sparse array
'''
class Solution:
    def largestVariance(self, s: str) -> int:
        char_pos = defaultdict(list)
        for i,c in enumerate(s):
            char_pos[c].append(i)
        res = 0
        for a in char_pos.keys():
            for b in char_pos.keys():
                if a == b:
                    continue
                A, B = len(char_pos[a]), len(char_pos[b])
                a_idx = b_idx = 0
                running_sum = 0
                has_a = has_b = False
                while a_idx < A or b_idx < B:
                    if a_idx < A and (b_idx == B or char_pos[a][a_idx] < char_pos[b][b_idx]):
                        running_sum += 1
                        has_a = True
                        a_idx += 1
                    elif b_idx < B:
                        running_sum -= 1
                        has_b = True
                        b_idx += 1
                    if running_sum < 0 and a_idx < A and b_idx < B:
                        running_sum = 0
                        has_a = has_b = False
                    if has_a and has_b:
                        res = max(res, running_sum)
        return res
'''
                    

4822