Group Anagrams

Med
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Group Anagrams

Approach

For each string, sort its characters to create a canonical key. Use a hash map where the key is the sorted string and the value is a list of original strings sharing that key. Strings that are anagrams of each other produce the same sorted key.

Complexity Analysis

Time
O(n * k log k)
Space
O(n * k)

Pattern

Hash Map Grouping

Why It Works

Anagrams are permutations of the same characters, so sorting them produces an identical string. This sorted form serves as a unique group identifier in the hash map.

Updated Feb 2026