forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-resultant-array-after-removing-anagrams.py
60 lines (49 loc) · 1.17 KB
/
find-resultant-array-after-removing-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
49
50
51
52
53
54
55
56
57
58
59
60
# Time: O(n * l)
# Space: O(1)
import collections
# freq table
class Solution(object):
def removeAnagrams(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
result = []
prev = None
for x in words:
cnt = collections.Counter(x)
if prev and prev == cnt:
continue
prev = cnt
result.append(x)
return result
# Time: O(n * llogl)
# Space: O(l)
import collections
# sort
class Solution2(object):
def removeAnagrams(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
result = []
prev = None
for x in words:
s = sorted(x)
if prev and prev == s:
continue
prev = s
result.append(x)
return result
# Time: O(n * llogl)
# Space: O(l)
import collections
# sort
class Solution3(object):
def removeAnagrams(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
return [words[i] for i in xrange(len(words)) if i == 0 or sorted(words[i-1]) != sorted(words[i])]