All problems

Word Pattern

Easy · hash map

You are given a string pattern made of lowercase letters, and a sentence s made of lowercase words separated by single spaces.

The sentence follows the pattern if you can pair each letter of the pattern with a word so that:

Return true if the sentence follows the pattern, and false otherwise. If the pattern has a different number of letters than the sentence has words, the answer is false.

Examples

Example 1

Input: pattern = "abba", s = "red blue blue red"

Output: true

Pair a with red and b with blue.

Example 2

Input: pattern = "abba", s = "red blue blue green"

Output: false

The first and last letters are both a, but the first and last words differ.

Example 3

Input: pattern = "abab", s = "go go go go"

Output: false

Letters a and b would both have to be paired with go.

Constraints