1/*2Time: O(26*26*n)3Space: O(1)4Tag: Kadane's Algorithm5Difficulty: H (Logic) | E(Implementation)6*/78class Solution {9public:10int largestVariance(string s) {11int res = 0;12for (int i = 0; i < 26; i++) {13for (int j = 0; j < 26; j++) {14if (i == j) continue;15int highFreq = 0;16int lowFreq = 0;17bool prevHadLowFreqChar = false;18for (char ch : s) {19if (ch - 'a' == i)20highFreq++;21else if (ch - 'a' == j)22lowFreq++;23if (lowFreq > 0)24res = max(res, highFreq - lowFreq);25else if (prevHadLowFreqChar)26res = max(res, highFreq - 1);27if (highFreq - lowFreq < 0) {28highFreq = 0;29lowFreq = 0;30prevHadLowFreqChar = true;31}32}33}34}35return res;36}37};