leetcode

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

1371.cpp (692B)


      1 class Solution {
      2     static const uint8_t map[27];
      3 
      4   public:
      5     int findTheLongestSubstring(const string &s) {
      6         static int um[1 << 5];
      7         memset(um, 0xFF, sizeof(um));
      8         int res = 0, crnt = 0;
      9         for (int i = 0; i < s.size(); i++) {
     10             crnt ^= (1 << map[s[i] & 0x1F]) >> 1;
     11             if (!crnt)
     12                 res = max(res, i + 1);
     13             else if (um[crnt] == -1)
     14                 um[crnt] = i;
     15             else
     16                 res = max(res, i - um[crnt]);
     17         }
     18         return res;
     19     }
     20 };
     21 
     22 const uint8_t Solution::map[27] = {0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0,
     23                                    0, 4, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0};