Skip to content
BASA DSA

Group Anagrams

MediumArrays & Hashing

Group strings by their letter multiset. Return every input string once, preserving duplicate occurrences; group order and order within groups are unrestricted.

Example 1

Input
strs = ["arc","car","rat","tar","x"]
Output
[["arc","car"],["rat","tar"],["x"]]

Explanation

arc/car share letters, rat/tar share letters, and x forms its own group.

Example 2

Input
strs = ["","a",""]
Output
[["",""],["a"]]

Explanation

Both empty strings survive as two occurrences in one group; a forms another.

Example 3

Input
strs = []
Output
[]

Explanation

No input strings means no groups.

Constraints

  • 0 <= number of strings <= 10000
  • Each string has 0..100 lowercase English letters

Function contractgroupAnagramsArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…