[LeetCode] Find the Difference

389. Find the Difference

You are given two strings s and t.

String t is generated by random shuffling string s and then add one more letter at a random position.

Return the letter that was added to t.

1
2
3
4
5
6
7
8
class Solution {
public:
char findTheDifference(string& s, string& t) {
char c = 0;
for(int i = 0; i < s.length(); i++) c ^= s[i] ^ t[i];
return c ^ t[t.length() - 1];
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/07/PS/LeetCode/find-the-difference/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.