标签:
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
return(sorted(s)==sorted(t))
dic1[item] = dic1.get(item, 0) + 1
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
dic1,dic2={},{}
for item in s:
dic1[item] = dic1.get(item,0)+1
for item in t:
dic2[item] dic.get(item,0)+1
return (dic1==dic2)
from collections import defaultdict #标准字典库
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
if len(s) != len(t): #首先判断长度是否相同
return False
else:
dic = defaultdict(int)
for i in range(len(t)):
dic[s[i]] +=1
"""
s[i]为字符串中的字母,dic[s[i]]为字母所对应的key值
"""
dic[t[i]] -=1
for key,value in dic.iteritems():
if value !=0:
return False
return True
标签:
原文地址:http://blog.csdn.net/neoye125/article/details/51329596