#K55282. Find and Replace Pattern

    ID: 29941 Type: Default 1000ms 256MiB

Find and Replace Pattern

Find and Replace Pattern

You are given a list of words and a pattern string. A word matches the pattern if there exists a bijective mapping (a one-to-one correspondence) between letters such that replacing each letter of the pattern with the corresponding letter yields the word.

In other words, for a word to match the pattern there must exist a permutation of letters that transforms the pattern into the word. Formally, for two strings \(w\) (word) and \(p\) (pattern) of equal length, a match exists if there is a bijection \(f\) satisfying \(w_i = f(p_i)\) for all \(i\).

Your task is to output all words that match the given pattern. Input and output are handled via standard input (stdin) and standard output (stdout).

inputFormat

The input is read from standard input and has the following format:

  • The first line contains an integer n, representing the number of words.
  • The next n lines each contain a single word.
  • The final line contains the pattern string.

outputFormat

Print a single line containing all words that match the pattern, separated by a single space. If no words match, print an empty line.

## sample
6
abc
deq
mee
aqq
dkd
ccc
abb
mee aqq