Skip to content

Latest commit

 

History

History
36 lines (24 loc) · 813 Bytes

290.Word Pattern.md

File metadata and controls

36 lines (24 loc) · 813 Bytes

题目

根据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))