mediumArrays & HashingAI-applied~20 min
Does a Prompt Template Follow a Token Pattern
A pattern of placeholder letters should correspond one-to-one with a sequence of tokens (words). Decide whether the token sequence follows the pattern under a consistent bijection.
Problem
Given a pattern string and a string s of space-separated words, return true if s follows the pattern: there is a bijection between letters in pattern and words in s such that the i-th letter maps to the i-th word consistently.
Input
A string pattern and a space-separated string s.
Output
A boolean: whether s follows the pattern.
Constraints
- 1 <= pattern.length <= 300
- 1 <= s.length <= 3000
- pattern is lowercase letters; s is lowercase words separated by single spaces.
Examples
Example 1
Input
pattern = "abba", s = "dog cat cat dog"
Output
true
a<->dog, b<->cat is a consistent bijection.
Example 2
Input
pattern = "abba", s = "dog cat cat fish"
Output
false
a maps to both dog and fish, breaking the bijection.