leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2982.cpp (676B)


      1 class Solution {
      2   public:
      3     int maximumLength(const string &s) const {
      4         static int count[26][3];
      5         int cnt = 0, prev = s[0];
      6 
      7         memset(count, 0xFF, sizeof(count));
      8         for (const char c : s) {
      9             if (c == prev)
     10                 cnt++;
     11             else {
     12                 prev = c;
     13                 cnt = 1;
     14             }
     15 
     16             const int idx = c - 'a';
     17             auto mini = min_element(count[idx], count[idx] + 3);
     18             if (cnt > *mini) *mini = cnt;
     19         }
     20 
     21         int res = -1;
     22         for (int i = 0; i < 26; i++) {
     23             res = max(res, *min_element(count[i], count[i] + 3));
     24         }
     25 
     26         return res;
     27     }
     28 };