LeetCode 389. Find the Difference

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
char findTheDifference(string s, string t) {
int bits = 0;
for (char c : s)
bits ^= (c - 'a');
for (char c : t)
bits ^= (c - 'a');
return bits + 'a';
}
};
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
char findTheDifference(string s, string t) {
unordered_map<char, int> m;
for (char c : s)
++m[c];
for (char c : t)
if (--m[c] < 0)
return c;
return ' ';
}
};
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
char findTheDifference(string s, string t) {
int as = 0, at = 0;
for (char ch: s)
as += (ch - 'a');
for (char ch: t)
at += (ch - 'a');
return at - as + 'a';
}
};