Given a pattern
and a string str
, find if str
follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern
and a non-empty word in str
.
Notes:
You may assume pattern
contains only lowercase letters, and str
contains lowercase letters separated by a single space.
class Solution(object): def wordPattern(self, pattern, str): """ :type pattern: str :type str: str :rtype: bool """ pattern_list = list(pattern) str_list = str.split(" ") if (len(pattern_list) != len(str_list)): return False pattern_set = set(pattern_list) str_set = set(str_list) if (len(pattern_set) != len(str_set)): return False set_ = set(zip(pattern_list,str_list)) if (len(pattern_set) != len(set_)): return False return True
以上