My Python Solution


  • 0
    C
        map, string = {}, str.split()
        if len(string) != len(pattern):
            return False
    
        for letter, word in zip(pattern, string):
            if letter not in map:
                if word in map.values():
                        return False
                map[letter] = word
            elif map[letter] != word:
                return False
    
        return True

  • 0
    A

    I am thinking this line: if word in map.values():
    this actually will go through every values in map so it take O(n) not O(1), right?


  • 0
    C

    Yes, u are right, it takes O(n) so the above method is much slower. Use a set to store the word is a better idea


Log in to reply
 

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