Loading...
Loading...
A college blog lets students add short tags to posts. Two tags belong together when one is a rearrangement of the other's letters, such as listen and silent.
Put the tags into groups so that tags in the same group use exactly the same letters the same number of times. Inside each group, list the tags in alphabetical order. Then print the groups in alphabetical order of their first tag. A tag that appears twice is listed twice.
Example 1
Input
6 listen silent enlist google gogole cat
Output
3 cat enlist listen silent gogole google
Three groups. cat comes first because it is alphabetically before enlist and gogole.
Example 2
Input
3 ab ba ab
Output
1 ab ab ba
All three tags use one a and one b. The repeated tag is kept.
Try the problem first. Open one hint at a time.
Two tags belong together when they look the same after sorting their letters.
Use the sorted letters as a key in a HashMap<String, List<String>> and add each tag to its key's list.
A HashMap has no fixed order, so sort each group, then sort the groups by their first tag before printing.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor