Python Solution Using Zip


  • 0

    This is basically checking whether there is a one to one mapping between pattern and str, a perfect case to use zip:

    class Solution(object):
    def wordPattern(self, pattern, str):
    """
    :type pattern: str
    :type str: str
    :rtype: bool
    """
    str,pattern = str.split(' '),list(pattern)
    if len(str) != len(pattern): return False
    d = zip(str,pattern)
    return len(set(d)) == len(set(str)) and len(set(d)) == len(set(pattern))


Log in to reply
 

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