← 返回 amazon 的题目列表Group Anagrams
类型:online_judge
Problem: Group Anagrams
Given an array of strings strs, group strings that are anagrams of one another and return all groups.
Two strings are anagrams if every lowercase English letter occurs the same number of times in both strings. The order of groups and the order of strings within a group do not matter.
Function Signature
def group_anagrams(strs: list[str]) -> list[list[str]]:
Example
Input:
["eat", "tea", "tan", "ate", "nat", "bat"]
One valid output:
[["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]
Constraints
1 <= len(strs) <= 100,000
0 <= len(strs[i]) <= 100
Strings contain only lowercase English letters.
Provide a solution that improves on sorting every string to create a hash key.
Example
Input
6
eat
tea
tan
ate
nat
bat
Output
[eat tea ate]
[tan nat]
[bat]