Problem 70
Word Pattern
Easy · 10 pts · String, Hash Table
Problem
You are given a pattern of letters and a list of words. Decide whether the words follow the pattern: there must be a one-to-one mapping between the pattern's letters and the words. Each letter always stands for the same word, different letters stand for different words, and the number of words must equal the length of the pattern. Walk through the letters and words together, keeping a map from letters to words and another from words to letters. Any conflict in either map means the answer is NO.
Input
First line: the pattern (lowercase letters). Second line: words separated by spaces.
Output
Print YES or NO.
Constraints
1 <= pattern length <= 300; 1 <= number of words <= 300
Example input
abba dog cat cat dog
Example output
YES
Explanation
The pattern "abba" with "dog cat cat dog" maps a → dog and b → cat. Every position agrees with this mapping, and dog and cat are different words, so the answer is YES.