class Solution(object): def wordPattern(self, pattern, str): """ :type pattern: str :type str: str :rtype: bool """ length = len(pattern) strings = str.split() if len(strings) != length: return False "pattern strings 检查长度是否匹配" table = dict() for letter, word in zip(pattern, strings): if letter in table: if word == table[letter]: continue else: return False table[letter] = word del table "每一个单词都会对应唯一的字母" table = dict() for letter, word in zip(pattern, strings): if word in table: if letter == table[word]: continue else: return False table[word] = letter "每一个字母都会对应唯一的单词"