<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:
    """User Submitted Solution:"""
    def largestVariance(self, s: str) -> int:
        answer = 0
        
        counter = collections.Counter(s)
        
        for ch1 in counter.keys():
            for ch2 in counter.keys():
                if ch1 == ch2:
                    continue

                diff = 0
                has_ch2 = False
                remain_ch2_count = counter[ch2]
                for ch in s:
                    if ch == ch1:
                        diff += 1
                    elif ch == ch2:                        
                        diff -= 1
                        has_ch2 = True
                        remain_ch2_count -= 1
                        if diff < 0 and remain_ch2_count > 0:
                            diff = 0
                            has_ch2 = False

                    if has_ch2:
                        answer = max(answer, diff)

        return answer

    # """My Solution:"""
    # # def largestVariance(self, s: str) -> int:
    #     all_chars = list(set(s))

    #     if len(all_chars) == 1:
    #         return 0

    #     res = 0
    #     for i in range(len(all_chars)):
    #         x = all_chars[i]
    #         for j in range(i + 1, len(all_chars)):
    #             y = all_chars[j]
    #             res = max(
    #                 res,
    #                 self.solve_two_chars(s, x, y),
    #                 self.solve_two_chars(s, y, x)
    #             )
    #     return res
        
    # def solve_two_chars(self, s, x, y):
    #     # treat x = +1 and y = -1
    #     # then find the maximum sum of a subarray
    #     # that is just Kadane's algorithm
    #     # note that we require a modification to Kadane's. This is
    #     # because in our problem we can only consider substrings
    #     # that have at least one occurrence of x and at least
    #     # one occurrence of y
    #     # so we modify Kadane's in the following fashion
    #     # recall that standard Kadane's solves for each index i in the array the subproblem of
    #     # the largest sum ending at index i
    #     # we solve this standard subproblem. but we additionally also solve for each index i the
    #     # subproblem of the largest sum ending at index i such that the subarray corresponding to
    #     # this sum has at least one occurrence of y
    #     # so we are solving two subproblems simultaneously
    #     # to solve the second subproblem, we just need to make a small observation. note that if
    #     # s[i] is y, then the answer to the latter subproblem at index i equals the answer to the
    #     # original subproblem at index i
    #     # and if s[i] != y, then the subarray that is the answer to the latter subproblem must include
    #     # some indices j < i, and so we just use the the answer to the latter subproblem at index i-1
    #     # and add in the value s[i].
    #     # since we initialize the latter subproblem at -infinity, this works
    #     res = 0
    #     best_sum_including_y_ending_at_i = -float("inf")
    #     best_sum_ending_at_i = 0
    #     for c in s:
    #         num = 1 * (c == x) - 1 * (c == y)

    #         # standard Kadane's
    #         best_sum_ending_at_i = max(
    #             best_sum_ending_at_i + num,
    #             num
    #         )

    #         # if num is -1, then the answer is just the best_sum_ending_at_i
    #         # because we know it includes a -1
    #         if num < 0:
    #             best_sum_including_y_ending_at_i = best_sum_ending_at_i
    #         # otherwise we must include the best_sum_including_y_ending_at_i
    #         # and then add in num (which only increases since it is >= 0)
    #         else:
    #             best_sum_including_y_ending_at_i += num

    #         res = max(res, best_sum_including_y_ending_at_i)
    #     return res
3770