Skip to main content
Back to problems
Leetcode
Medium
Hash Maps
Strings
Sorting
Google
Meta
Group Anagrams

Group strings that are anagrams of each other into separate lists.

Acceptance 92%
Problem Statement

Given an array of strings, group together the strings that are anagrams. Two strings are anagrams if they contain the same characters with the same frequencies, possibly in a different order.

Return the groups in any order. The order of strings within each group also does not matter.

Input Format

  • An array of lowercase strings strings.
  • Each string contains only English letters.

Output Format

  • Return a list of groups.
  • Each group contains all strings from the input that are anagrams of one another.

Constraints

  • 1≤strings.length1 \leq strings.length
  • Strings are non-empty.
  • All characters are lowercase English letters.
  • The answer may be returned in any order.
Examples
Sample cases returned by the problem API.

Example 1

Input

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

Output

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

Explanation

Words with the same multiset of letters are placed in the same group.

Example 2

Input

[""]

Output

[[""]],

Explanation

A single string forms one group by itself.

Show 1 more example

Example 3

Input

["a"]

Output

[["a"]]

Explanation

A single-character string is an anagram only of itself in this input.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.