<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>

from collections import Counter
class Solution:   
    def largestVariance(self, s: str) -> int:
      freq = Counter(s)
      letters = set(s)
      global_max = 0
      for a in letters:
        for b in letters:
          if a==b: continue
          local_max = 0
          num_a = 0
          num_b = 0
          remain_b = freq[b]
          for c in s:
            if c == a:
              num_a += 1              
            if c == b:
              num_b += 1
              remain_b -= 1
            if c in [a,b]:
              local_max = num_a - num_b
              if num_b > 0:
                global_max = max(local_max, global_max)
              if local_max<0 and remain_b>0:
                num_a = 0
                num_b = 0
      return global_max
            



# class Solution:
    # 
    # def largestVariance(self, s: str) -> int:
    #     letters = set(s)
    #     occurrences = {c: [] for c in letters}
    #     for i, c in enumerate(s):
    #         occurrences[c].append(i)
        
    #     # Alias
    #     occ = occurrences
        
    #     best = 0
    #     for a in letters:
    #         for b in letters:
    #             if a == b: continue
    #             ptr_a = 0
    #             ptr_b = 0
    #             occ_a = occ[a]
    #             occ_b = occ[b]
    #             len_a = len(occ_a)
    #             len_b = len(occ_b)
    #             found_a = False
    #             found_b = False

    #             best_sum = float('-inf')
    #             max_curr = 0
    #             while ptr_a < len_a and ptr_b < len_b:
    #                 if occ_a[ptr_a] < occ_b[ptr_b]:
    #                     ptr_a += 1
    #                     max_curr += 1
    #                     if found_b: best_sum = max(max_curr, best_sum)
    #                     else: best_sum = max(max_curr - 1, best_sum)
    #                 else:
    #                     found_b = True
    #                     ptr_b += 1
    #                     max_curr -= 1
    #                     if max_curr < 0: 
    #                         max_curr = 0
    #                         found_b = False
                
    #             max_curr += len_a - ptr_a
    #             best_sum = max(max_curr - (0 if found_b else 1), best_sum)
    #             print(f"max variance between {a} and {b} is {best_sum}")
    #             best = max(best_sum, best)
    #     return best
3349