Leetcode解题-Group Anagrams

描述

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from bisect import bisect


class Solution(object):
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""

d = {}
for s in strs:
key = ''.join(sorted(s))
d.setdefault(key, [])
rs = d[key]
idx = bisect(rs, s)
rs.insert(idx, s)
return d.values()

热评文章