forked from TARANG0503/DSA-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroup words with same set of characters
50 lines (41 loc) · 1.39 KB
/
Group words with same set of characters
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
47
48
49
50
# Python program to print all words that
# have the same unique character set
# Function to group all strings with same characters
from collections import Counter
def groupStrings(input):
# traverse all strings one by one
# dict is an empty dictionary
dict={}
for word in input:
# sort the current string and take it's
# sorted value as key
# sorted return list of sorted characters
# we need to join them to get key as string
# Counter() method returns dictionary with frequency of
# each character as value
wordDict=Counter(word)
# now get list of keys
key = wordDict.keys()
# now sort these keys
key = sorted(key)
# join these characters to produce key string
key = ''.join(key)
# now check if this key already exist in
# dictionary or not
# if exist then simply append current word
# in mapped list on key
# otherwise first assign empty list to key and
# then append current word in it
if key in dict.keys():
dict[key].append(word)
else:
dict[key]=[]
dict[key].append(word)
# now traverse complete dictionary and print
# list of mapped strings in each key separated by ,
for (key,value) in dict.items():
print (','.join(dict[key]))
# Driver program
if __name__ == "__main__":
input=['may','student','students','dog','studentssess','god','cat','act','tab','bat','flow','wolf','lambs','amy','yam','balms','looped','poodle']
groupStrings(input)