class Solution(object): def wordPattern(self, pattern, str): """ :type pattern: str :type str: str :rtype: bool """ # split str into single words. slist = str.split() # here we need to consider the len difference # eg: pattern:"aaa" str:"aa aa aa aa" if len(pattern) != len(slist): return False # just like we do in isomorphic problem. return (len(set(pattern)) == len(set(slist)) == len(set(zip(pattern, slist))))
A Simple Python Solution Using BIF zip(), 36ms
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.