Skip to content

Commit cfe3d42

Browse files
authored
Create find-resultant-array-after-removing-anagrams.py
1 parent 091b37e commit cfe3d42

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Time: O(n * l)
2+
# Space: O(1)
3+
4+
import collections
5+
6+
7+
# freq table
8+
class Solution(object):
9+
def removeAnagrams(self, words):
10+
"""
11+
:type words: List[str]
12+
:rtype: List[str]
13+
"""
14+
result = []
15+
prev = None
16+
for x in words:
17+
cnt = collections.Counter(x)
18+
if prev and prev == cnt:
19+
continue
20+
prev = cnt
21+
result.append(x)
22+
return result
23+
24+
25+
# Time: O(n * llogl)
26+
# Space: O(l)
27+
import collections
28+
29+
30+
# sort
31+
class Solution2(object):
32+
def removeAnagrams(self, words):
33+
"""
34+
:type words: List[str]
35+
:rtype: List[str]
36+
"""
37+
result = []
38+
prev = None
39+
for x in words:
40+
s = sorted(x)
41+
if prev and prev == s:
42+
continue
43+
prev = s
44+
result.append(x)
45+
return result

0 commit comments

Comments
 (0)