Description

Given a pattern and a string s, find if s follows the same pattern. Here follow means a full match, such that there is a bijection between pattern and words in s.

Examples

Input:pattern = "abba", s = "dog cat cat dog"
Output:true
Explanation:

Bijection exists.

Input:pattern = "abba", s = "dog cat cat fish"
Output:false
Explanation:

Pattern maps a->dog and b->cat, but then a should map to dog while the 4th word is "fish".

Input:pattern = "abc", s = "red blue red"
Output:false
Explanation:

Pattern 'a' maps to 'red', 'b' maps to 'blue', but 'c' also tries to map to 'red'. Since 'red' is already mapped to 'a', the bijection fails.

Constraints

  • 1 ≤ pattern.length ≤ 300

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!