#K14171. Group Anagrams
Group Anagrams
Group Anagrams
You are given T test cases. For each test case, you are given an integer k and k strings. Your task is to group these strings into lists of anagrams. Two strings are anagrams if and only if their sorted characters are identical. In other words, for two words \(w_1\) and \(w_2\), they are anagrams if \(\text{sort}(w_1)=\text{sort}(w_2)\).
For each test case, you need to output a JSON-formatted array of anagram groups. Each group must have its words sorted in lexicographical order. Finally, the groups themselves should be sorted by their first element in lexicographical order.
Note: Read input from stdin
and write output to stdout
.
inputFormat
The input is given via stdin and has the following format:
T k1 s11 s12 ... s1k k2 s21 s22 ... s2k ... kT sT1 sT2 ... sTk
Here, the first line contains an integer T representing the number of test cases. For each test case, the first line contains an integer k (the number of strings), followed by a line with k space-separated strings.
outputFormat
For each test case, output one line to stdout containing a JSON-formatted array of anagram groups. Each anagram group is represented as an array of strings sorted in lexicographical order, and the groups themselves are sorted by the lexicographical order of their first element.
For example, if a test case contains the words eat tea ate tan nat
, a valid output is:
[["ate", "eat", "tea"], ["nat", "tan"]]## sample
1
5
eat tea tan ate nat
[["ate","eat","tea"],["nat","tan"]]
</p>