#K34107. Anagrams Dictionary

    ID: 25236 Type: Default 1000ms 256MiB

Anagrams Dictionary

Anagrams Dictionary

Given a list of words from standard input, group them by their sorted character order (anagrams). For each group, output the key as the sorted letters and the associated list of words, all sorted alphabetically.

Input: The first line contains an integer \(n\) representing the number of words. The second line contains \(n\) space-separated words.

Output: Print the resulting dictionary in JSON format. The keys of the dictionary (the sorted words) must be in lexicographical order, and the lists of words for each key must also be sorted in lexicographical order.

Mathematical Formulation: For each word \(w\), define its sorted form as \(S(w)=\mathrm{sort}(w)\). Group all words with the same \(S(w)\) together.

inputFormat

The first line contains an integer \(n\) (the number of words). The second line contains \(n\) space-separated words.

outputFormat

Output the resulting dictionary as a JSON object. The keys (sorted words) must be in lexicographical order, and the corresponding list of words for each key must be sorted lexicographically.

## sample
8
listen silent enlist rat tar art hello hell
{"art": ["art", "rat", "tar"], "ehll": ["hell"], "ehllo": ["hello"], "eilnst": ["enlist", "listen", "silent"]}