mediumHashingPattern: Hash Map / Grouping

Anagram Cluster Formation Solution

Problem Statement

Given an array of strings, group the anagrams together and print each group on a new line.

Examples

Example 1:
Input:["eat","tea","tan","ate","nat","bat"]
Output:[["eat","tea","ate"],["tan","nat"],["bat"]]
Explanation: Anagrams are grouped together

Constraints

  • 1 <= n <= 10^4
  • Strings consist of lowercase English letters.
Time: O(N K log K) Space: O(N K)
Use a HashMap where the key is the sorted string and the value is a list of original strings. Iterate, sort each string, group by key. Time O(N * K log K), Space O(N * K).

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.