leetcode

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

0859.cpp (676B)


      1 class Solution {
      2   public:
      3     bool buddyStrings(const string &s, const string &goal) {
      4         int a = -1, b = -1, dup = 0, count[26] = {0};
      5         if (s.size() != goal.size()) return false;
      6         for (int i = 0; i < s.size(); i++) {
      7             if (count[s[i] & 0xF]) dup = 1;
      8             count[s[i] & 0xF] = 1;
      9             if (s[i] != goal[i]) {
     10                 if (a == -1)
     11                     a = i;
     12                 else if (b == -1)
     13                     b = i;
     14                 else
     15                     return false;
     16             }
     17         }
     18         if (a == -1) return dup;
     19         if (b == -1) return false;
     20         return s[a] == goal[b] && s[b] == goal[a];
     21     }
     22 };