-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path49.字母异位词分组.py
More file actions
46 lines (46 loc) · 1008 Bytes
/
49.字母异位词分组.py
File metadata and controls
46 lines (46 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#
# @lc app=leetcode.cn id=49 lang=python
#
# [49] 字母异位词分组
#
# https://leetcode-cn.com/problems/group-anagrams/description/
#
# algorithms
# Medium (58.44%)
# Likes: 185
# Dislikes: 0
# Total Accepted: 28.5K
# Total Submissions: 48.6K
# Testcase Example: '["eat","tea","tan","ate","nat","bat"]'
#
# 给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。
#
# 示例:
#
# 输入: ["eat", "tea", "tan", "ate", "nat", "bat"],
# 输出:
# [
# ["ate","eat","tea"],
# ["nat","tan"],
# ["bat"]
# ]
#
# 说明:
#
#
# 所有输入均为小写字母。
# 不考虑答案输出的顺序。
#
#
#
class Solution(object):
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
anagram={}
for str in strs:
key=tuple(sorted(str))
anagram[key]=anagram.get(key,[])+[str]
return anagram.values()