题目:
给定两个字符串,判断这两个字符串是否由相同的字符组成。若是,则返回True,否则返回False。
解题思路:
考虑使用哈希表来提高效率。
代码(Python):
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
Dict = {}
for i in s:
if i in Dict:
Dict[i] = Dict[i]+1
else:
Dict[i] = 1
for j in t:
if j in Dict:
if Dict[j]==1:
Dict.pop(j)
else:
Dict[j] = Dict[j]-1
else:
return False
if len(Dict)==0:
return True
else:
return False