-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathanagrams.py
More file actions
46 lines (31 loc) · 1.45 KB
/
anagrams.py
File metadata and controls
46 lines (31 loc) · 1.45 KB
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
# Anagrams : https://leetcode.com/problems/anagrams/
# Given an array of strings, return all groups of strings that are anagrams.
# Note: All inputs will be in lower-case.
# Solution
# ========
# (1) list filtering : http://www.diveintopython.net/power_of_introspection/filtering_lists.html
# (2) list filering : http://www.thegeekstuff.com/2014/05/python-filter-and-list/
# (3) Collections counter : http://pymotw.com/2/collections/counter.html
# (4) python lambda : http://www.secnetix.de/olli/Python/lambda_functions.hawk
import collections
class Solution:
# @param strs, a list of strings
# @return a list of strings
def anagrams(self, strs):
count = collections.Counter([''.join(sorted(str)) for str in strs])
# (1) when using sorted() on a string, it gets sorted and becomes a list of chacters
# (2) join them back to form a string
# (3) counter returns a dictionary of word:count
# print "Count == " , count
# print "strs == " , strs
# for elem in strs:
# print elem,count[''.join(sorted(elem))]
return_list = [ elem for elem in strs if count[''.join(sorted(elem))]>1]
# filter the list , such that the count[elem] > 1
return return_list
#return filter(lambda x: count[''.join(sorted(x))] > 1, strs)
#Testing Code
#==============
# my_solution = Solution()
# strs = ['Hello','elloH','oHell','world','first','program']
# print my_solution.anagrams(strs)