#K76157. Bijective Mapping Check
Bijective Mapping Check
Bijective Mapping Check
You are given a pattern string S and a string of space-separated words. Your task is to determine if there exists a bijective mapping between letters in the pattern and the words. A bijective mapping means that each unique letter in the pattern is mapped to a unique word and vice versa.
Formally, given a pattern \(S\) of length \(n\) and a list of words \(W\) of length \(n\), there exists a bijective mapping if and only if for every index \(i\) (\(1 \leq i \leq n\)), the mapping satisfies:
[ \text{if } S_i = S_j \text{ then } W_i = W_j, \quad \text{and if } S_i \neq S_j \text{ then } W_i \neq W_j. ]
You will be given several test cases as input through stdin
, and for each test case you should print a single line with the result (true
or false
) to stdout
.
inputFormat
The input is given via stdin
in the following format:
T pattern_1 words_1 pattern_2 words_2 ... pattern_T words_T
Where:
T
is the number of test cases.- For each test case, the first line is the pattern string and the second line is a space‐separated list of words.
outputFormat
For each test case, print a single line to stdout
with either true
if a bijective mapping exists between the pattern and the list of words, or false
otherwise.
2
abba
dog cat cat dog
abba
dog cat cat fish
true
false
</p>