naive solution: using hash table. since diff between s and t is one char only, first count time of char in s, then decrement char according to t, if char is out of count range or never shown in hashtable, return that char
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
d = {}
for c in s:
d[c] = d.get(c,0) + 1
for c in t:
#this covers the case of 1. c not in d 2. c in d but has one more than the c in s.
if d.get(c,0) == 0:
return c
d[c]-= 1
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
res = 0
for c in s+t:
res ^= ord(c)
return chr(res
optimization, instead of using bit manipulation,use diff
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
res = 0
for c in t:
res += ord(c)
for c in s:
res -= ord(c)
return chr(res)