MMeesho·DSASDE-1Coding Screen
Group Anagrams Together
Problem
Given an array of strings, group all anagrams together. Return groups in any order.
Example
Input: ["eat","tea","tan","ate","nat","bat"]
Output: [["eat","tea","ate"],["tan","nat"],["bat"]]
Constraints
- 1 ≤ strs.length ≤ 10^4
- 0 ≤ strs[i].length ≤ 100
- Strings consist of lowercase English letters
Approach
Use a sorted string (or character frequency tuple) as the hashmap key. O(n·k·log k) where k is max string length.
Follow-up
Can you solve it in O(n·k) using a character count key instead of sorting?
added 6 days ago