Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  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. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement