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.

The output order does not matter as long as they are grouped correctly.

Input

  • strings: a list of strings.

Output

A list of string lists representing the grouped anagrams.

Examples

Example 1:

Input:

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

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

Explanation:

Constraints

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

Try it yourself

Solution

Title

Script

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Contrary to popular belief, Lorem Ipsum is not simply random text.

  >>> a = [1, 2, 3]
  >>> a[-1]
  3

Get premium for instant access to all content and solutions

Upgrade