Find the Difference

easy string hash map bit manipulation

Problem

Strings s and t are anagrams except t has exactly one extra letter inserted at a random position. Return that extra letter.

Inputs = "abcd", t = "abcde"
Output"e"
'e' is the character that appears in t but not in s.

def find_the_difference(s, t):
    x = 0
    for c in s: x ^= ord(c)
    for c in t: x ^= ord(c)
    return chr(x)
function findTheDifference(s, t) {
  let x = 0;
  for (const c of s) x ^= c.charCodeAt(0);
  for (const c of t) x ^= c.charCodeAt(0);
  return String.fromCharCode(x);
}
class Solution {
    public char findTheDifference(String s, String t) {
        int x = 0;
        for (char c : s.toCharArray()) x ^= c;
        for (char c : t.toCharArray()) x ^= c;
        return (char) x;
    }
}
char findTheDifference(string s, string t) {
    int x = 0;
    for (char c : s) x ^= c;
    for (char c : t) x ^= c;
    return (char) x;
}
Time: O(n) Space: O(1)