We're given a pattern like "abba" and a sentence like "dog cat cat dog". The question is whether there's a one-to-one correspondence between letters in the pattern and words in the sentence. 'a' always maps to the same word, 'b' always maps to the same word, and no two different letters map to the same word.
The tricky part is that the mapping must be bijective, meaning it goes both ways. It's not enough for each letter to consistently map to the same word. We also need each word to consistently map to the same letter. For example, pattern "ab" and string "dog dog" should return false because both 'a' and 'b' would map to "dog", violating the bijection.
This means we need to track two mappings at once: one from characters to words, and another from words to characters. If either mapping is violated, the answer is false.
1 <= pattern.length <= 300 and 1 <= s.length <= 3000. Both inputs are small, so an O(n + m) single pass is comfortable and there is no pressure to micro-optimize.pattern contains only lowercase English letters, so there are at most 26 distinct characters. A fixed-size 26-entry array could replace the char-to-word hash map if desired.Walk through the pattern and words in parallel, maintaining a mapping in both directions. When we see a pattern character and a word together for the first time, we create a bidirectional mapping. If we see them again, we verify the existing mapping still holds. If a character was previously mapped to a different word, or a word was previously mapped to a different character, the bijection is broken.
Why two maps and not one? A single map only enforces one direction. If we track only char-to-word, pattern "ab" with "dog dog" passes incorrectly: 'a' -> "dog" is fine, and 'b' -> "dog" creates no conflict in a char-to-word map. But the bijection is violated because "dog" is now mapped to two different characters. The word-to-char map catches exactly this case: when 'b' arrives, "dog" already maps to 'a', so the mismatch fails the check. Each map guards one direction of the correspondence, and the problem requires both.
s into an array of words.pattern, return false.charToWord (character -> string) and wordToChar (string -> character).i, get the current pattern character and the current word.charToWord, check that it maps to the current word. If not, return false.wordToChar, check that it maps to the current character. If not, return false.true.This approach directly models the bijection with two explicit maps. The next approach reaches the same answer differently: instead of storing what each character and word map to, it encodes the repetition structure of each sequence and compares those structures position by position.
Two sequences follow the same pattern if and only if they have the same first-occurrence structure. For pattern "abba", the first-occurrence indices are [0, 1, 1, 0]: 'a' first appears at index 0, 'b' first appears at index 1, the second 'b' was already seen at index 1, and the second 'a' was already seen at index 0. If "dog cat cat dog" produces the same sequence [0, 1, 1, 0], the pattern matches.
This replaces the explicit bijection check with a single structural comparison. At each position we compare the first-occurrence index of the pattern character against the first-occurrence index of the word, and they must agree.
The first-occurrence index at position i is the same for the character and the word exactly when both are "new" together or both are "repeats of the same earlier position" together. If the character is new at i (its first index is i) but the word repeats from an earlier position j < i, the indices differ and we reject. This rules out two characters mapping to one word and one character mapping to two words at the same time, so a single index comparison enforces the bijection in both directions without two separate maps.
s into words. If the count doesn't match pattern length, return false.false.true.