Word Pattern

Easy

Given a pattern and a string s, return true when s follows the same pattern.

There must be a bijection between a letter in pattern and a non-empty word in s.

Example 1

wordPattern()
Input
pattern = "abba", s = "dog cat cat dog"
Output
true

Example 2

wordPattern()
Input
pattern = "abba", s = "dog cat cat fish"
Output
false
Constraints
1 <= pattern.length <= 300
1 <= s.length <= 3000
Words in `s` are separated by single spaces.

Topics

StringsHash Table

Implement wordPattern in the editor and return the answer — printing it is never graded.