SHARE
TWEET

Untitled

a guest Aug 23rd, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Time Complexity: O(n), where n is s.size() + t.size()
  2. // Space Complexity: O(1)
  3. class Solution {
  4. public:
  5.     char findTheDifference(string s, string t) {
  6.         int k = 0;
  7.        
  8.         assert(t.size() - s.size() == 1);
  9.        
  10.         for (auto c : t) {
  11.             k ^= 1 << (c - 'a');
  12.         }
  13.         for (auto c : s) {
  14.             k ^= 1 << (c - 'a');
  15.         }
  16.         for (int i = 1; i <= 26; i++) {
  17.             if (k == (1 << i)) {
  18.                 return i + 'a';
  19.             }
  20.         }
  21.         return 'a';
  22.     }
  23. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top