描述
Given an array of strings, group anagrams together.
For example, given: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”],
Return:[
[“ate”, “eat”,”tea”],
[“nat”,”tan”],
[“bat”]
]
Note:
For the return value, each inner list’s elements must follow the lexicographic order.
All inputs will be in lower-case.
分析
用哈希表,理论上可以时间O(nlogn)
,空间O(n)
。我们下面的实现由于用了数组而不是链表,所以要慢一些,最坏情况O(n^2)
。
代码
Python
1 | from bisect import bisect |