#C14610. Find Anagrams

    ID: 44279 Type: Default 1000ms 256MiB

Find Anagrams

Find Anagrams

Given a list of words and a target word, output all the words from the list that are anagrams of the target. Two words are anagrams if and only if their sorted characters are identical. In other words, for a word w and target t, they are anagrams if and only if \( sorted(w)=sorted(t) \).

Note: The comparison is case-sensitive. This means that uppercase and lowercase letters are considered distinct.

For example:

  • Input: words = ["listen", "silent", "enlist", "abc", "def"], target = "listen" → Output: "listen silent enlist"
  • Input: words = ["race", "care", "acer", "hello", "world"], target = "care" → Output: "race care acer"

inputFormat

The input is provided via stdin in the following format:

<n>
<word1> <word2> ... <word_n>
<target>

Where:

  • n is an integer denoting the number of words.
  • The second line contains n space-separated words.
  • The third line contains the target string.

outputFormat

Output a single line to stdout with the anagrams found in the order they appear in the input, separated by a single space. If no anagrams are found, output an empty line.

## sample
5
listen silent enlist abc def
listen
listen silent enlist