Given a pattern and a string str
, find if str
follows the same pattern.
Here follows means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str
.
Examples:
pattern = "abba"
, str = "dog cat cat dog"
should return true; dog
is a
, cat
is b
and the words form the abba
pattern.
pattern = "abba"
, str = "dog cat cat fish"
should return false; the string follows a abbc
pattern instead.
My solution works in Python 2:
def wordPattern(self, pattern, str):
s = pattern
t = str.split()
return map(s.find, s) == map(t.index, t)
But I am just wondering why this solution does not working in Python 3. There, the function will always return False
when trying to test the above examples. Could anyone please give some advice?