<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 {
public:
    int getVal(string &s, char ch1, char ch2){
        // cout<<ch1<<"**"<<ch2<<'\n';
        int maxiTillHere = 0;
        int INF = -1e6;
        int ans = INF;
        vector<int> prev(2, INF);
        for(char ch : s){
            if(ch == ch1){
                int maxi1 = max(max(prev[0]+1, prev[1]+1), 1);
                int maxi2 = (prev[1] == INF) ? INF : prev[1] + 1;
                ans = max(ans, maxi2);
                prev[0] = maxi1;
                prev[1] = maxi2;
            }
            else if(ch == ch2){
                int maxi1 = (prev[0] == INF) ? INF : prev[0] - 1;
                int maxi2 = max(max(prev[0]-1, prev[1]-1), -1);
                ans = max(ans, maxi1);
                prev[0] = maxi1;
                prev[1] = maxi2;
            }
            // cout<< prev[0]<<" "<<prev[1]<<'\n';
        }
        // cout<<ans<<'\n';
        return ans;
    }
    int largestVariance(string s) {
        unordered_set<char> st;
        vector<char> arr;
        for(char ch: s){
            if(st.find(ch)!=st.end()) continue;
            else st.insert(ch), arr.push_back(ch);
        }
        int size = arr.size();
        int ans = 0;
        for(int i = 0; i<size; i++){
            for(int j = 0; j<size; j++){
                if(i!=j) {
                    int val = getVal(s, arr[i], arr[j]);
                    ans = max(ans, val);
                }
            }
        }
        return ans;
    }
};
11600