SHARE
TWEET

Untitled

a guest Aug 23rd, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Time Complexity: O(n) where n is t.size() + s.size()
  2. // Memory Complexity: O(1)
  3. class Solution {
  4. public:
  5.     char findTheDifference(string s, string t) {
  6.         vector<int> counters(256, 0);
  7.        
  8.         assert(t.size() - s.size() == 1);
  9.        
  10.         for (auto c : t) {
  11.             counters[c - 'a']++;
  12.         }
  13.         for (auto c : s) {
  14.             counters[c - 'a']--;
  15.         }
  16.         for (int i = 0; i < 256; i++) {
  17.             if (counters[i] > 0) {
  18.                 return 'a' + i;
  19.             }
  20.         }
  21.         assert(false);
  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