PIXELBANKv9.1.0
Menu

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
  • strs[i] is lowercase English letters
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Group Anagrams - Medium | PixelBank