Decent Python Solution


  • 0
    M
    class Solution(object):
        def wordPattern(self, pattern, str):
            """
            :type pattern: str
            :type str: str
            :rtype: bool
            """
            if len(pattern) != len(str.split(' ')):
                return False
            
            d, used = dict(), set()
            for p, s in zip(pattern, str.split(' ')):
                if p in d:
                    if d[p] != s:
                        return False
                    else:
                        continue
                elif s in used:
                    return False
                else:
                    used.add(s)
                    d[p] = s
            return True
    

Log in to reply
 

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