You are given a pattern string of lowercase letters and a sentence of lowercase words separated by single spaces. Decide whether there is a bijection (a one-to-one correspondence) between the distinct pattern letters and the distinct words such that replacing each letter of the pattern with its matched word reproduces the sentence exactly.
Concretely, this holds when the pattern and the sentence have the same length and, for all positions i and j, pattern[i] == pattern[j] if and only if word[i] == word[j].
Print YES if such a bijection exists, otherwise print NO.
Line 1: the pattern string (lowercase letters, no spaces).
Line 2: the sentence (space-separated lowercase words).
One line: YES or NO.
a-z).Example 1
Input
abba dog cat cat dog
Expected
YES
Explanation
Mapping a->dog and b->cat is consistent and one-to-one, and reproduces the sentence, so the answer is YES.
Example 2
Input
abba dog cat cat fish
Expected
NO
Explanation
The letter a would need to map to both 'dog' (position 0) and 'fish' (position 3), a conflict, so the answer is NO.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →