forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanagrams.py
More file actions
21 lines (20 loc) · 702 Bytes
/
anagrams.py
File metadata and controls
21 lines (20 loc) · 702 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# -*- coding: utf-8 -*-
class Solution:
# @param strs: A list of strings
# @return: A list of strings
def anagrams(self, strs):
# write your code here
ret = []
word_map = {}
for word in strs:
sorted_word = ''.join(sorted(word))
if sorted_word not in word_map:
word_map[sorted_word] = {'count':1, 'words':[word]}
else:
word_map[sorted_word]['count'] += 1
word_map[sorted_word]['words'].append(word)
for key, info in word_map.items():
if info['count'] > 1:
for word in info['words']:
ret.append(word)
return ret