classSolution(object): deffindTheDifference(self, s, t): """ :type s: str :type t: str :rtype: str """ dic_t = {} for item in t: dic_t[item] = dic_t.get(item,0) + 1 for item in s: dic_t[item] = dic_t[item] - 1 for item in t: if dic_t[item] == 1: return item
这题同样可以使用 XOR,或者只是将字符转换为对于对应的 ASCII 数值然后相减
XOR
1 2 3
classSolution(object): deffindTheDifference(self, s, t): returnchr(reduce(operator.xor, map(ord, s + t)))
相减
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution(object): deffindTheDifference(self, s, t): """ :type s: str :type t: str :rtype: str """ ans = 0 for c in t: ans = ans + ord(c) for c in s: ans = ans - ord(c) returnchr(ans)