Word Pattern

Implement wordPattern

Given a pattern of characters and a space-separated string s, determine whether s follows the same pattern — where each character in pattern maps to exactly one word in s, and each word maps back to exactly one character (a full one-to-one, bijective mapping).

Example 1:

Input: pattern = "xyxz", s = "cat dog cat bird"

Output: true

Example 2:

Input: pattern = "xyx", s = "sun moon moon"

Output: false

Example 3:

Input: pattern = "xy", s = "sun sun"

Output: false

+ 6 hidden test cases run on Submit.

Constraints:

  • 1 ≤ pattern.length ≤ 10
  • pattern consists of lowercase English letters
  • s consists of lowercase English letters and single spaces, with no leading or trailing spaces
  • 1 ≤ number of words in s ≤ 10

pattern =

xyxz

s =

cat dog cat bird