forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_anagram.py
54 lines (39 loc) · 1.48 KB
/
group_anagram.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
52
53
54
"""
author: Aayush Soni
Given an array of strings strs, group the anagrams together.
You can return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of
a different word or phrase, typically using all the original letters exactly once.
Leetcode link: https://leetcode.com/problems/group-anagrams/description/
"""
def group_anagrams(words):
"""
Group anagrams in a list of words.
This function takes a list of words and groups them
based on whether they are anagrams of each other.
Examples:
>>> group_anagrams(["cat", "dog", "tac", "god", "act"])
{'act': ['cat', 'tac', 'act'], 'dgo': ['dog', 'god']}
>>> group_anagrams(["listen", "silent", "hello", "world"])
{'eilnst': ['listen', 'silent'], 'ehllo': ['hello'], 'dlorw': ['world']}
"""
grouped_words = {}
# Put all anagram words together in a dictionary
# where the key is the sorted word
for word in words:
sorted_word = "".join(sorted(word))
if sorted_word not in grouped_words:
grouped_words[sorted_word] = [word]
else:
grouped_words[sorted_word].append(word)
return grouped_words
if __name__ == "__main__":
words = ["cat", "dog", "tac", "god", "act"]
groups = group_anagrams(words)
# Sort the groups for consistent output
sorted_groups = sorted(groups)
for key in sorted_groups:
group = groups[key]
print(" ".join(group))
import doctest
doctest.testmod()