From b5c87065a4c77cc960dd1f852ccd6aba1185e11f Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Sun, 12 Oct 2025 18:31:26 -0600 Subject: 2273. find resultant array after removing anagrams --- find_restaurant_arr_rem_anagram.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 find_restaurant_arr_rem_anagram.py (limited to 'find_restaurant_arr_rem_anagram.py') diff --git a/find_restaurant_arr_rem_anagram.py b/find_restaurant_arr_rem_anagram.py new file mode 100644 index 0000000..2cb4b8a --- /dev/null +++ b/find_restaurant_arr_rem_anagram.py @@ -0,0 +1,36 @@ +# 2273. Find Resultant Array After Removing Anagrams + +""" +you are given a 0-indexed string array 'words' where 'words[i]' consists of +lowercase english letters. in one operation, select any index 'i' such that +'0 < i < words.length' and 'words[i - 1]' and 'words[i]' are anagrams, and +delete 'words[i]' from 'words'. keep performing this operation as long as yu +can select an index that satisfies the conditions. return 'words' after +performining all operations. it can be shown that selecting the indices for +each operation in any arbitrary order will lead to the same result. an +anagram is a word or phrase formed by rearranging the letters of a different +word or phrase using all the original letters exactly once. +""" + + +class Solution(object): + def anagram(self, s, t): + return sorted(s) == sorted(t) + + def removeAnagrams(self, words): + """ + :type words: List[str] + :rtype: List[str] + """ + ans = [] + for i in words: + if ans and self.anagram(ans[-1], i): + continue + ans.append(i) + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.removeAnagrams(words=["abba", "baba", "bbaa", "cd", "cd"])) + print(obj.removeAnagrams(words=["a", "b", "c", "d", "e"])) -- cgit v1.2.3