#-*- coding: UTF-8 -*-
class Solution(object):
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: bool
"""
tag=0
tagdic={}
tagList=[]
i=0
while i<len(pattern):
if tagdic.has_key(pattern[i]):
tagList.append(tagdic.get(pattern[i]))
else:
tagdic.setdefault(pattern[i],tag)
tagList.append(tag)
tag+=1
i+=1
strList=str.split(' ')
tagdic={};tag=0;tagList2=[];i=0
while i<len(strList):
if tagdic.has_key(strList[i]):
tagList2.append(tagdic.get(strList[i]))
else:
tagdic.setdefault(strList[i],tag)
tagList2.append(tag)
tag+=1
i+=1
return tagList==tagList2
sol=Solution()
print sol.wordPattern('abba', 'dog cat cat dog')
相关文章
- [leetcode]Word Ladder II @ Python
- 290. Word Pattern【LeetCode by java】
- [LeetCode]题解(python):140-Word Break II
- 【LeetCode】819. Most Common Word 解题报告(Python)
- Java [Leetcode 290]Word Pattern
- 【LeetCode】140. Word Break II 解题报告(Python & C++)
- 【一天一道LeetCode】#290. Word Pattern
- 【leetcode❤python】 58. Length of Last Word
- [LeetCode] Word Pattern II 词语模式之二
- [LeetCode&Python] Problem 748. Shortest Completing Word