Advertisement
Guest User

Untitled

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