所有Leetcode题目不定期汇总在 Github, 欢迎大家批评指正,讨论交流。
class Solution:
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
# Approach one :
# if set(s) == set(t) and len(s) == len(t):
# for i in set(s):
# if s.count(i) != t.count(i):
# return False
# return True
# return False
# Approach two:一种更优雅的完成方式 all(iterable):当 iterable 中所有元素都为 True 时(或者 iterable 为空),返回 True 。
# return set(s) == set(t) and all(s.count(i) == t.count(i) for i in set(s))
# Approach three
return collections.Counter(s) == collections.Counter(t)
所有Leetcode题目不定期汇总在 Github, 欢迎大家批评指正,讨论交流。