Word Pattern

Given a pattern string *pattern* and a sentence *s* (a string of words separated by spaces), return `true` if *s* follows the same pattern. Here *s* follows the pattern if there is a bijection between a letter in *pattern* and a word in *s*. Return `false` otherwise.

Input Format

pattern = "pattern", s = "sentence string"

Output Format

boolean (true/false)

Constraints

  • 1 <= pattern.length <= 300; s contains lowercase words separated by spaces; length of s is at most 3000 characters.

Examples

Example 1:

Input:

pattern = "abba"
s = "dog cat cat dog"

Output:

true

Explanation:

a->dog, b->cat is a valid mapping.

Example 2:

Input:

pattern = "abba"
s = "dog cat cat fish"

Output:

false

Explanation:

Mapping would require a->dog, b->cat, but fish does not match.

Loading...
Word Pattern - Hashing DSA Problem