Problem Statement
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
An anagram is a word formed by rearranging the letters of another word.
Approach
Use sorted string as key in hash map. All anagrams will have the same sorted form.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | O(n * k log k) where k is max string length |
| Space | O(n * k) |
Examples
Example 1
Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
Explanation: Anagrams are grouped together
Example 2
Input: strs = [""]
Output: [[""]]
Example 3
Input: strs = ["a"]
Output: [["a"]]
Constraints
- βΈ
1 <= strs.length <= 10^4 - βΈ
0 <= strs[i].length <= 100 - βΈ
strs[i]consists of lowercase English letters
Loading...
Sign in to run your code...
Asked by companies:
AmazonFacebookMicrosoftApple