首页 > 其他 > 详细

查找表_leetcode290

时间:2019-03-17 14:53:50      阅读:146      评论:0      收藏:0      [点我收藏+]
# 解题思路:字典解决其对应关系 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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!