Skip to content
LC-2273 Easy LeetCode

2273. Find Resultant Array After Removing Anagrams

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 59% Topics: Array, Hash Table, String, Sorting
View full problem on LeetCode
Reference solution (spoiler · python)
# 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])]

Solution from kamyu104/LeetCode-Solutions · MIT