#K53957. Find Anagram Groups
Find Anagram Groups
Find Anagram Groups
Given a target string and a list of words, find all words from the list which are anagrams of the target string. Two strings are anagrams if they contain the same characters with the same frequency. Note that the target string should be processed in a case‐insensitive manner (i.e. converted to lower case before comparison). For example, if the target string is listen
, then its sorted letters are \( \text{eilnst} \), and words like enlist
, inlets
, and silent
are anagrams.
Input: The input consists of multiple lines. The first line contains the target string. The second line contains a non-negative integer \( n \) which represents the number of words. The following \( n \) lines each contain one word.
Output: If there are any anagrams of the target string in the list, print them in the order of appearance separated by a single space. If there are no anagrams, print an empty line.
Note: The solution should use the standard input (stdin) and standard output (stdout) for input and output respectively.
inputFormat
The input from standard input (stdin) is structured as follows:
- The first line is a string target.
- The second line is an integer n indicating the number of words that follow.
- The next n lines each contain one word.
outputFormat
The output to standard output (stdout) should be a single line containing the anagram group (if any) found in the list. The words must be printed in the same order they appear in the input and separated by a single space. If no anagrams are found, output an empty line.
## samplelisten
5
enlist
google
inlets
banana
silent
enlist inlets silent