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 a letter in pattern and a non-empty word in s. Specifically: - Each letter in pattern maps to exactly one unique word in s. - Each unique word in s maps to exactly one letter in pattern. - No two letters map to the same word, and no two words map to the same letter.
Example 1
Input: pattern = abba, s = dog cat cat dog
Output: true
Explanation: Map a ↔ dog and b ↔ cat. The pattern a b b a then spells dog cat cat dog, matching s exactly.
Example 2
Input: pattern = abba, s = dog cat cat fish
Output: false
Explanation: The first 'a' maps to "dog" but the last 'a' would have to map to "fish". A letter cannot map to two different words.
Example 3
Input: pattern = aaaa, s = dog cat cat dog
Output: false
Explanation: Every letter is 'a', but the words include both "dog" and "cat", so 'a' would need to map to two different words.
Split s into words and pair each pattern letter with a word. Maintain a letter-to-word map and a word-to-letter map; any disagreement in either direction means the pattern isn't followed.
Run checks all cases above. Submit evaluates all test cases.