Ancient Cipher
InterviewTime limit2sMemory limit128 MB
Given two equal-length strings of capital letters, decide whether the first can be obtained from the second by a substitution cipher followed by a permutation.
- Level
Medium4 of 10
- Topics
- String, Sorting, Hash map, Implementation
- Solved
- No attempts yet
Problem
The ancient Roman empire had a strong government system with various departments, including a secret service department. Important documents were sent between the provinces and the capital in encrypted form to prevent eavesdropping. The most popular ciphers of those times were the so-called substitution cipher and permutation cipher.
A substitution cipher changes every occurrence of each letter into some other letter. The substitutes for all letters must be different, but for some letters the substitute may coincide with the original letter. For example, applying a substitution cipher that changes every letter from 'A' to 'Y' into the next letter of the alphabet, and changes 'Z' into 'A', to the message "VICTORIOUS" yields the message "WJDUPSJPVT".
A permutation cipher applies some permutation to the letters of the message. For example, applying the permutation to the message "VICTORIOUS" yields the message "IVOTCIRSUO".
It was quickly noticed that, applied separately, both the substitution cipher and the permutation cipher were rather weak. But combined, they were strong enough for those times. Thus, the most important messages were first encrypted with a substitution cipher, and the result was then encrypted with a permutation cipher. Encrypting the message "VICTORIOUS" with the combination of the ciphers described above yields the message "JWPUDJSTVP".
Archeologists have recently found a message engraved on a stone plate. At first glance it seemed completely meaningless, so it was suggested that the message had been encrypted with some substitution and permutation ciphers. They have conjectured the possible text of the original message, and now they want to check their conjecture. They need a computer program to do it, so you have to write one.
Input
The input contains two lines. The first line contains the message engraved on the plate. Before encrypting, all spaces and punctuation marks were removed, so the encrypted message contains only capital letters of the English alphabet. The second line contains the original message that is conjectured to be encrypted in the message on the first line; it also contains only capital letters of the English alphabet.
The two lines have equal length, which does not exceed .
Output
Output "YES" if the message on the first line could be the result of encrypting the message on the second line, or "NO" otherwise.