這題是找出2個字元串不同的部分,隻是多了一個字母。注意順序有可能會錯亂。使用collections包裡面的Counter子產品輕松搞定。
from collections import Counter
class Solution(object):
def findTheDifference(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
if s == '':
return t
a = Counter(s)
b = Counter(t)
for i in b.keys():
if a[i] != b[i]:
return i