Skip to content

Commit 8e6c7fe

Browse files
committed
乱序字符串
1 parent 8a3d98f commit 8e6c7fe

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

anagrams.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Solution:
4+
# @param strs: A list of strings
5+
# @return: A list of strings
6+
def anagrams(self, strs):
7+
# write your code here
8+
ret = []
9+
word_map = {}
10+
for word in strs:
11+
sorted_word = ''.join(sorted(word))
12+
if sorted_word not in word_map:
13+
word_map[sorted_word] = {'count':1, 'words':[word]}
14+
else:
15+
word_map[sorted_word]['count'] += 1
16+
word_map[sorted_word]['words'].append(word)
17+
for key, info in word_map.items():
18+
if info['count'] > 1:
19+
for word in info['words']:
20+
ret.append(word)
21+
return ret

0 commit comments

Comments
 (0)