Python 98.92%

  • 0

    I used a python dictionary and a set.

    class Solution(object):
        def wordPattern(self, pattern, str):
            :type pattern: str
            :type str: str
            :rtype: bool
            dic, seen = {}, set()
            str = str.split(' ')
            if len(str) != len(pattern):
                return False
            for i in range(len(str)):
                if pattern[i] not in dic:
                    if str[i] in seen:
                        return False
                    dic[pattern[i]] = str[i]
                    if dic[pattern[i]] != str[i]:
                        return False
            return True

Log in to reply

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