Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Time Complexity: O(n), where n is s.size() + t.size()
- // Space Complexity: O(1)
- class Solution {
- public:
- char findTheDifference(string s, string t) {
- int k = 0;
- assert(t.size() - s.size() == 1);
- for (auto c : t) {
- k ^= 1 << (c - 'a');
- }
- for (auto c : s) {
- k ^= 1 << (c - 'a');
- }
- for (int i = 1; i <= 26; i++) {
- if (k == (1 << i)) {
- return i + 'a';
- }
- }
- return 'a';
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement