All problems
Word Pattern
Problem
Return True if the pattern string matches the space-separated words in text, with a bijection between pattern characters and words.
Split the text on whitespace and require the same length as the pattern. Use mappings in both directions, exactly like isomorphic strings.
Hint
Python split with no arguments collapses runs of spaces and handles the empty string.
Test cases
case 1
in: ["abba","dog cat cat dog"]
expected: true
case 2
in: ["abba","dog cat cat fish"]
expected: false
case 3
in: ["aaaa","dog cat cat dog"]
expected: false
case 4
in: ["ab","dog dog"]
expected: false
case 5
in: ["",""]
expected: true
Starter codePython
def word_pattern(pattern, text):
# Your code here
pass5 test cases · run your code against them in the editor