给定两个字符串 s 和 t,它们只包含小写字母。

字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。

请找出在 t 中被添加的字母。

示例:

输入:
s = "abcd"
t = "abcde"

输出:
e

解释:
'e' 是那个被添加的字母。

class Solution(object):
def findTheDifference(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
dic = dict()
for n in s:
dic[n] = dic.get(n, 0) + 1
for n in t:
if n in dic:
dic[n] = dic.get(n, 0) - 1
if dic[n] == 0:
del dic[n]
else:
return n

用一个字典来保存每个字母出现的次数。 循环第二个字符串的时候,判断该字母出现在第一个字典中的情况,如果没有出现,那么该字母就是目标,如果出现,那么先减1,如果是0就删除,这样第二个次数多的时候就会找不到了。