-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroup_Anagrams.py
48 lines (38 loc) · 1.76 KB
/
Group_Anagrams.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
# 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.
# Example 1:
# Input: strs = ["eat","tea","tan","ate","nat","bat"]
# Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
# Example 2:
# Input: strs = [""]
# Output: [[""]]
# Example 3:
# Input: strs = ["a"]
# Output: [["a"]]
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
answer = []
# we will sort each string inside the list in a seperate list
# then we will go through this list and if we find an equivalent string, we take
# a string from the original list, instead of the "sorted" list: we will
# take advantage of positional indexing
sorted_list = []
for word in strs:
sorted_str = ''.join(sorted(word))
sorted_list.append(sorted_str)
# sorted strings will determine what strings to pick from the strs
# in order not to repeatedly pick the same anagrams of the sorted word
# we will use a set
Set = set()
for i in range(0, len(strs)):
anagrams = []
for j in range(i, len(strs)):
if sorted_list[i] == sorted_list[j] and sorted_list[j] not in Set:
anagrams.append(strs[j])
# indicator will hold the sorted string that serves as the source of anagrams
indicator = sorted_list[j]
# if there is at least one word in the anagrams list
if anagrams:
answer.append(anagrams)
Set.add(indicator)
return answer