Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-134

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
    pass

5 test cases · run your code against them in the editor