leetcode

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

0890.cpp (831B)


      1 class Solution {
      2   public:
      3     vector<string> findAndReplacePattern(const vector<string> &words, const string &pattern) {
      4         vector<string> res;
      5         static int um[27] = {0};
      6         static bool used[27] = {0};
      7         for (const auto &word : words) {
      8             for (int i = 0; i < pattern.size(); i++) {
      9                 const uint8_t w = word[i] & 0x1F;
     10                 const uint8_t p = pattern[i] & 0x1F;
     11                 if (um[w]) {
     12                     if (um[w] != p) goto next;
     13                     continue;
     14                 }
     15                 if (used[p]) goto next;
     16                 used[p] = true;
     17                 um[w] = p;
     18             }
     19             res.push_back(word);
     20         next:;
     21             memset(um, 0x00, sizeof(um));
     22             memset(used, 0x00, sizeof(used));
     23         }
     24 
     25         return res;
     26     }
     27 };