Skip to content

Latest commit

 

History

History

290-WordPattern

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Word Pattern

Problem can be found in here!

Solution: Hash Table

def wordPattern(pattern: str, s: str) -> bool:
    words = s.split()
    if len(words) != len(pattern) or len(set(words)) != len(set(pattern)):
        return False

    word_to_pattern = dict()
    for char, word in zip(pattern, words):
        if word not in word_to_pattern:
            word_to_pattern[word] = char
        elif word_to_pattern[word] != char:
            return False

    return True

Time Complexity: O(n), Space Complexity: O(n)