Skip to content

Commit 570b682

Browse files
authored
Create groupAnagrams.py
1 parent 8ee69dd commit 570b682

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Kangli/Strings/groupAnagrams.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution(object):
2+
def groupAnagrams(self, strs):
3+
d = collections.defaultdict(list)
4+
for s in strs:
5+
d["".join(sorted(s))].append(s)
6+
return [ group for group in d.values() ]
7+
8+
# use a hash, sort each string and use it as a key. Append unsorted string to list of values.
9+
# return all values

0 commit comments

Comments
 (0)