← 返回 uber 的题目列表Group Anagrams
类型:online_judge
Problem: Group Anagrams
Given an array of strings strs, group all anagrams together and return the grouped result.
Two strings are anagrams if they contain the same characters with the same frequencies.
Requirements
Implement an optimal-time solution.
Analyze the space complexity.
Discuss how to handle very large datasets.
Input Format
First line: integer n, the number of strings.
Next n lines: one lowercase English string per line.
Output Format
For deterministic judging:
Sort strings within each group lexicographically.
Sort all groups by the first string in each group.
Print a JSON-style 2D array.
Constraints
1 <= n <= 100000
0 <= len(strs[i]) <= 100
Total length of all strings is at most 1000000.
Example
Input:
6
eat
tea
tan
ate
nat
bat
Output:
[["ate", "eat", "tea"], ["bat"], ["nat", "tan"]]
Example
Input
6
eat
tea
tan
ate
nat
bat
Output
[["ate", "eat", "tea"], ["bat"], ["nat", "tan"]]