Python solution using dictionary

  • 0
    class Solution(object):
    def wordPattern(self, pattern, str):
        :type pattern: str
        :type str: str
        :rtype: bool
        l_str = str.split(" ")
        if len(pattern)!=len(l_str): return False
        n_str = len(set(l_str))
        n_pattern = len(set(pattern))
        if n_pattern!=n_str: return False
        d_pattern = {}
        for key in pattern:
            d_pattern[key] = []
        for i in range(len(l_str)):
        for key,value in d_pattern.items():
            if len(set(value))!=1: return False
        return True

Log in to reply

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