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

        res = 0  # To store the maximum variance found
        # Iterate over all pairs of distinct characters in the string
        for ch1 in set(s):
            for ch2 in set(s):
                if ch1 == ch2:  # Skip if the characters are the same
                    continue
                ch2_front = False  # Indicates whether a 'ch2' character has appeared before 'ch1'
                has_ch2 = False    # Indicates whether we have encountered a 'ch2' character at all
                var = 0            # Variance tracker for the current substring
                # Iterate over the characters of the string
                for i in range(len(s)):
                    if s[i] == ch1:  # Increment variance if current char is ch1
                        var += 1
                    if s[i] == ch2:  # Decrement variance if current char is ch2
                        has_ch2 = True
                        var -= 1
                        # If ch2_front is True and variance is not less than -1, adjust accordingly
                        if ch2_front and var >= -1:
                            ch2_front = False
                            var += 1
                        elif var < 0:  # If variance drops below 0, reset the front tracking
                            ch2_front = True
                            var = -1
                    # Update the result with the maximum variance found
                    res = max(res, var if has_ch2 else 0)
        return res


                
                
        
5558