PIXELBANKv8.2.1
Menu

Group Anagrams

Given an array of strings, group the anagrams together.

Output groups one per line, words space-separated and sorted, groups sorted by first word.

Example:

Input:
eat,tea,tan,ate,nat,bat
Output:
ate eat tea
bat
nat tan
Reasoning:
  • First, we identify the anagrams in the input array: "eat", "tea", and "ate" are anagrams, "tan" and "nat" are anagrams, and "bat" has no anagrams.
  • Then, we sort the words within each anagram group and sort the groups themselves based on the first word in each group.
  • The sorted anagram groups are: ["ate", "eat", "tea"], ["bat"], and ["nat", "tan"].
  • Finally, we output each group on a new line, with the words in each group separated by spaces, resulting in: ate eat tea, bat, and nat tan becomes bat and nat tan is sorted to nat tan.

Constraints:

  • 1 <= len(strs) <= 10^4
  • 0 <= len(strs[i]) <= 100
Editor

Test Results

0/0
Run code to see test results.