Problem Statement
leetcode problem link
Brute Force [Accepted]
class Solution:
def removeAnagrams(self, words: List[str]) -> List[str]:
N = len(words)
res = [words[0]]
for i in range(1, N):
prev_word = ''.join(sorted(list(words[i - 1])))
curr_word = ''.join(sorted(list(words[i])))
if prev_word != curr_word:
res.append(words[i])
return res
Editorial
Approach: Judge Individually
class Solution:
def removeAnagrams(self, words: List[str]) -> List[str]:
res = [words[0]] # result array
n = len(words)
# determine if two words are anagrams
def compare(word1: str, word2: str) -> bool:
freq = [0] * 26
for ch in word1:
freq[ord(ch) - ord("a")] += 1
for ch in word2:
freq[ord(ch) - ord("a")] -= 1
return all(x == 0 for x in freq)
for i in range(1, n):
if compare(words[i], words[i - 1]):
continue
res.append(words[i])
return res