dsa · easy
Word Pattern
pattern is a string of letters. s is a sentence of words separated by single spaces. Return whether the words follow pattern: the i-th letter maps to the i-th word, one-to-one, same as isomorphic strings but between letters and words.
Arguments
pattern— letter patterns— space-separated words
Example
pattern = "abba", s = "dog cat cat dog" → true.
pattern = "abba", s = "dog cat cat fish" → false.
Constraints
1 <= pattern.length <= 300 1 <= s.length <= 3000 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: "abba" "dog cat cat dog" Expected: true
Example 2
Input: "abba" "dog cat cat fish" Expected: false
Example 3
Input: "aaaa" "dog cat cat dog" Expected: false