根据pattern和str,找到是否符合这个pattern匹配模式。
举例:
- pattern = "abba", str = "dog cat cat dog" should return true.
- pattern = "abba", str = "dog cat cat fish" should return false.
- pattern = "aaaa", str = "dog cat cat dog" should return false.
- pattern = "abba", str = "dog dog dog dog" should return false.
注意:
假设pattern只包含小写字母,而str包含由空格分隔的小写字母。
对于python,巧妙使用map和find函数即可。
python:
class Solution:
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: bool
"""
t = str.split(' ')
return list(map(pattern.find, pattern)) == list(map(t.index, t))