A legacy device protocol encodes each message as a sequence of n integer symbol codes. A migration tool needs to check whether an old message and a new message could have come from the same original data under some one-to-one relabeling of symbol codes — that is, whether there exists a bijection f such that applying f to every code in the old sequence produces the new sequence exactly (equal codes in the old sequence must map to equal codes in the new sequence, and different codes in the old sequence must map to different codes in the new sequence).
Print YES if such a bijection exists, otherwise print NO.
Line 1: an integer n — the length of both sequences.
Line 2: n space-separated integers — the old sequence.
Line 3: n space-separated integers — the new sequence.
YES if a valid one-to-one relabeling exists, otherwise NO.
Example 1
Input
4 5 7 5 9 1 2 1 3
Expected
YES
Explanation
5→1, 7→2, 9→3 is a consistent one-to-one mapping (5 always maps to 1, and no two old codes map to the same new code), so the answer is YES.
Example 2
Input
3 1 2 1 4 4 5
Expected
NO
Explanation
The first two positions force 1→4, but the third position needs 1→5 — a contradiction. No valid mapping exists, 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 →