Simple Python Solution

  • 0
    class Solution(object):
        def wordPattern(self, pattern, str):
            m1 = [-1]*256
            m2 = {}
            content = str.split()
            for i in content:
                m2[i] = -1
            if len(pattern) != len(content):
                return False
            for i,v in enumerate(pattern):
                if m1[ord(v)] != m2[content[i]]:
                    return False
                m1[ord(v)] = i
                m2[content[i]] = i
            return True

Log in to reply

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