Group Anagrams

Given a list of strings, return a list of string lists that groups all anagrams together. Two strings are anagrams if rearranging one string results in another. For the purpose of this question, a string is an anagram of itself.

Each group of anagrams should be in alphabetical order. The output should be in alphabetical order by the first elements of each group of anagrams.

Input

  • strs: a list of strings.

Output

A list of string lists representing the grouped anagrams.

Examples

Example 1:

Input:

strs = ["eat" ,"tea", "tan", "ate", "nat", "bat"]

Output: [["ate", "eat", "tea"], ["bat"], ["nat", "tan"]]

Explanation:

Constraints

  • 1 <= len(strs) <= 10^4
  • 1 <= len(strs[i]) <= 100
  • Each word in the input consists of lowercase English letters, and they might not be unique.

Try it yourself

Solution

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro