You are given two strings
s
andt
.String
t
is generated by random shuffling strings
and then add one more letter at a random position.Return the letter that was added to
t
.
class Solution:
def findTheDifference(self, s, t):
c = 0
for char in t:
# 使用 ASCII 码记录
c ^= ord(char)
# ^ 计算,抵消相同数字 (x ^ x == 0)
for char in s:
c ^= ord(char)
return chr(c)
from collections import Counter
class Solution:
def findTheDifference(self, s, t):
c_s = Counter(s)
c_t = Counter(t)
for item in c_t:
# 分别比较每个字母出现的次数
if not c_t[item] == c_s[item]:
return item