def wordPattern(pattern, s):
dict1 = {}
dict2 = {}
for key,value in enumerate(list(s.split(" "))):
if value not in dict1:
dict1[value]= [key]
else:
dict1[value].append(key)
for key,value in enumerate(pattern):
if value not in dict2:
dict2[value]= [key]
else:
dict2[value].append(key)
return (list(dict1.values()) == list(dict2.values()))
import string
def word_to_pattern(word):
# Stores actual-letter to pattern-placeholder mapping
mapping = {}
# ZYXW... so we cap pop letters starting with A from the end
available_pattern_letters = list(string.ascii_uppercase)[::-1]
pattern = []
for letter in word.upper():
if letter not in string.ascii_uppercase:
# for punctuation etc
pattern.append(letter)
continue
if letter not in mapping:
# new letter we haven't seen yet in this word
mapping[letter] = available_pattern_letters.pop()
pattern.append(mapping[letter])
return pattern.join("")