边工作边刷题:70天一遍leetcode: day 55
Word Pattern
要点:和isomorphic string就是同一题。
错误点:
- 别忘了先比长度
class Solution(object):
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: bool
"""
hmap = {}
str = str.split()
if len(str)!=len(pattern):
return False
for i in xrange(len(pattern)):
if pattern[i] in hmap:
if str[i]!=hmap[pattern[i]]:
return False
else:
if str[i] in hmap.values():
return False
hmap[pattern[i]]=str[i]
return True