leetcode 389 Find the Difference 找不同 python 多種思路,最簡程式碼(collections.Counter()構建字典)
阿新 • • 發佈:2019-02-06
class Solution:
def findTheDifference(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
# method one 集合運算(考慮不到加入相同字母的情況)
# return "".join(set(t). - set(s))
# method two 一個蠢方法
# dic = {}
# for i in s:
# if i not in dic.keys():
# dic[i] = 1
# else:
# dic[i] = dic.get(i) + 1
# for j in t:
# if dic.get(j) != t.count(j):
# return j
# method three 呼叫預設的counter()類 可以直接構建字典。
# http://www.pythoner.com/205.html collections模組學習資料
return list(collections.Counter(t)-collections.Counter(s))[0]