A Simple Python Solution Using BIF zip(), 36ms


  • 5
    C
    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))))

  • 0
    This post is deleted!

  • 0
    W

    it's a very good method. help for me


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.