-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathPrintAnagrams.py
51 lines (35 loc) · 1.04 KB
/
PrintAnagrams.py
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
51
# Problem : https://practice.geeksforgeeks.org/problems/print-anagrams-together/1
# Input:
# N = 5
# words[] = {act,god,cat,dog,tac}
# Output:
# god dog
# act cat tac
# Explanation:
# There are 2 groups of
# anagrams "god", "dog" make group 1.
# "act", "cat", "tac" make group 2.
from collections import defaultdict
def Anagrams(words,n):
'''
words: list of word
n: no of words
return : list of group of anagram {list will be sorted in driver code (not word in grp)}
'''
#code here
anagrams = defaultdict(list)
for word in words:
anagrams["".join(sorted(word))].append(word)
return anagrams.values()
# Driver Code
if __name__ =='__main__':
t= int(input())
for tcs in range(t):
n= int(input())
words=input().split()
ans=Anagrams(words,n)
for grp in sorted(ans):
for word in grp:
print(word,end=' ')
print()
# Used default dict from collections module . It does not raise key value error