aboutsummaryrefslogtreecommitdiff
path: root/substr_largest_variance.cpp
blob: 6df445cc91c6937289d3018c90d561a4f9eff3f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// 2272. Substring With Largest Variance
#include "leetcode.h"

/*
 * the variance of a string is defined as the largest difference between the
 * number of ocurrences of any '2' characters present in the string. note the
 * two characters may or may not be the same. given a string 's', consisting of
 * lowercase english letters only, return the largest variance possible among
 * all substrings 's'. a substring is a contiguous sequence of characters witin
 * a string.
 */

class Solution {
public:
  int largestVariance(string s) {
    vector<int> arr(26);
    for (auto i : s)
      arr[i - 'a']++;
    int ans = 0;
    for (char i = 'a'; i <= 'z'; i++) {
      for (char j = 'a'; j <= 'z'; j++) {
        if (j == i || !arr[i - 'a'] || !arr[j - 'a'])
          continue;
        for (int k = 1; k <= 2; k++) {
          int a1 = 0, a2 = 0;
          for (auto l : s) {
            if (l == i)
              a1++;
            if (l == j)
              a2++;
            if (a2 > a1)
              a1 = 0, a2 = 0;
            if (a1 && a2)
              ans = max(ans, a1 - a2);
          }
          reverse(s.begin(), s.end());
        }
      }
    }
    return ans;
  }
};

int main() {
  Solution obj;
  printf("%d\n", obj.largestVariance("aababbb")); // expect: 3
  printf("%d\n", obj.largestVariance("abcde"));   // expect: 0
};