-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtbd.py
More file actions
102 lines (78 loc) · 3.12 KB
/
Copy pathtbd.py
File metadata and controls
102 lines (78 loc) · 3.12 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from collections import Counter
class Solution(object):
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
result, lookup, slow, fast, anagram = [], {}, 0, 1, []
# Have a lookup dict
# Use two pointers. Fast and slow
# Slow controls loop
# Fast checks each element
# If match, add both to result and lookup
# Move Slow to next element, provided it is not in lookup
# Reset Fast to index of next element after Slow
if len(strs) < 2:
result.append(strs)
return result
while slow < len(strs):
# if len(strs) == 1:
# return result.append(strs)
if fast < len(strs) and Counter(strs[slow]) == Counter(strs[fast]):
anagram.append(strs[fast])
lookup[strs[slow]] = slow
lookup[strs[fast]] = fast
strs.pop(fast)
else:
fast += 1
# End reached. Reset
if (fast >= len(strs) or slow + 1 > len(strs)):
anagram.insert(0, strs[slow])
result.append(anagram)
anagram = []
if slow + 1 < len(strs) and strs[slow + 1] in lookup:
slow += 2
fast = slow + 1
else:
slow += 1
fast = slow + 1
return result
# def groupAnagrams(self, strs):
# """
# :type strs: List[str]
# :rtype: List[List[str]]
# """
# result, lookup, slow, fast, anagram, reduction_dict = [], {}, 0, 1, [], {}
# # Have a lookup dict
# # Use two pointers. Fast and slow
# # Slow controls loop
# # Fast checks each element
# # If match, add both to result and lookup
# # Move Slow to next element, provided it is not in lookup
# # Reset Fast to index of next element after Slow
# reduction_dict = {k:k for k,v in enumerate(strs)}
# while slow < len(strs):
# if len(strs) == 1:
# return result.append(strs)
# if fast < len(strs) and Counter(strs[slow]) == Counter(strs[fast]):
# if fast in reduction_dict:
# anagram.append(strs[fast])
# lookup[strs[slow]] = slow
# lookup[strs[fast]] = fast
# del reduction_dict[slow]
# del reduction_dict[fast]
# fast += 1
# # End reached. Reset
# if (fast > len(strs) or slow + 1 > len(strs)):
# anagram.insert(0, strs[slow])
# result.append(anagram)
# anagram = []
# slow += 1
# return result
strs = ["eat","tea","tan","ate","nat","bat"]
sol_obj = Solution()
print(sol_obj.groupAnagrams(strs=strs))
print(sol_obj.groupAnagrams(strs=["a","a","a","a"]))
print(sol_obj.groupAnagrams(strs=["strs"]))
print(sol_obj.groupAnagrams(strs=[""]))