查找表_leetcode242
作者:互联网
# 解题思路:字典解决其对应关系 20190302 找工作期间
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
#字母异位词指字母相同,但排列不同的字符串
# dic1=collections.Counter(s)
# dic2=collections.Counter(t)
dic1 = dict()
dic2 = dict()
for i in range(len(s)):
if s[i] not in dic1:
dic1[s[i]] = 1
else:
dic1[s[i]] += 1
for j in range(len(t)):
if t[j] not in dic2:
dic2[t[j]] = 1
else:
dic2[t[j]] += 1
if len(dic1)!=len(dic2):#此种情况直接返回False
return False
for key,value in dic2.items():
if key in dic1 and value==dic1[key]:#key在dic1中并且值相等
continue
else:
return False
return True
标签:leetcode242,return,len,else,dic1,dic2,查找,key 来源: https://www.cnblogs.com/lux-ace/p/10546933.html