# Python Solution using Dictionaries

• ``````class Solution:
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: bool
"""

d = {}

str = str.split(" ")

pattern =list(pattern)

if len(str) != len(pattern):
return False

if len(str) == 1:
return True

for i in range(len(pattern)):
if pattern[i] not in d:
d[pattern[i]] = [i]
else:
val = d.get(pattern[i])
val.append(i)
d[pattern[i]] = val

l = list(d.keys())

for i in range(len(l)):
c=1
for j in range(len(d.get(l[i]))):
if j == 0:
w = str[d.get(l[i])[j]]
else:
if w == str[d.get(l[i])[j]]:
c+=1

if c != len(d.get(l[i])):
return False

d1={}

for i in range(len(str)):
if str[i] not in d1:
d1[str[i]] = [i]
else:
val = d1.get(str[i])
val.append(i)
d1[str[i]] = val

l1 = list(d1.keys())

#print(l1)
#print(d1)

for i in range(len(l1)):
c=1
for j in range(len(d1.get(l1[i]))):
if j == 0:
w = pattern[d1.get(l1[i])[j]]
else:
if w == pattern[d1.get(l1[i])[j]]:
c+=1

if c != len(d1.get(l1[i])):
return False

return True

``````

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