leetcode

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

1557.cpp (635B)


      1 class Solution {
      2   public:
      3     bool closeStrings(const string &word1, const string &word2) {
      4         if (word1.size() != word2.size()) return false;
      5         const int n = word1.size();
      6         int w1[27] = {0}, w2[27] = {0};
      7 
      8         for (int i = 0; i < word1.size(); i++) {
      9             w1[word1[i] & 0x1F]++;
     10             w2[word2[i] & 0x1F]++;
     11         }
     12 
     13         for (int i = 1; i <= 26; i++)
     14             if ((bool)w1[i] ^ (bool)w2[i]) return false;
     15 
     16         sort(begin(w1), end(w1));
     17         sort(begin(w2), end(w2));
     18 
     19         for (int i = 0; i < 26; i++)
     20             if (w1[i] != w2[i]) return false;
     21 
     22         return true;
     23     }
     24 };