📘
Group Anagrams
MediumHash Maps & Counting
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, andnat tanbecomesbatandnat tanis sorted tonat tan.
Constraints:
- 1 <= len(strs) <= 10^4
- 0 <= len(strs[i]) <= 100
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.