Group Anagrams
Medium · Arrays & Hashing · Hash Table · String · Sorting
Problem
Given an array of strings strs, group the anagrams together. Return the groups in any order, and the strings inside each group in any order.
Examples
Input: strs = ["eat","tea","tan","ate","nat","bat"] Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
Any ordering of the groups, or within a group, is accepted.
Input: strs = [""] Output: [[""]]
Constraints
- 1 <= len(strs) <= 10^4
- 0 <= len(strs[i]) <= 100
- strs[i] is lowercase English letters
Approaches
- Sorted string as the key — time O(n · k log k), space O(n · k)
Related problems
Start free on Cariara · Coding problems · System design studio