码迷,mamicode.com
首页 > 其他好文 > 详细

查找表_leetcode290

时间:2019-03-17 15:54:04      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:return   找工作   ==   ali   word   字典   pat   als   val   

# 解题思路:字典解决其对应关系 20190302 找工作期间
#使用字典,pattern当key,str当value,形成配对


class Solution(object):
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: boo
"""

s = str.split()

if len(pattern) != len(s):
return False

return len(set(zip(pattern, s))) == len(set(pattern)) == len(set(s))


class Solution(object):
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: bool
"""
#使用字典,pattern当key,str当value,形成配对
dic = {}
strToList= str.split()
if len(pattern) != len(strToList) or len(set(pattern)) != len(set(strToList)):
return False
for i, val in enumerate(pattern):
if val not in dic:
dic[val] = strToList[i]
elif dic[val] != strToList[i]:
return False
return True

查找表_leetcode290

标签:return   找工作   ==   ali   word   字典   pat   als   val   

原文地址:https://www.cnblogs.com/lux-ace/p/10546937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!