#2273
Find Resultant Array After Removing Anagrams
pupil · 305 · lc easy +21 · verified · 69.9% accepted · 1,060 likes · top 78%
Description
You are given a 0-indexed array words of lowercase English strings.
While any index i (with 0 < i < words.length) exists such that words[i - 1] and words[i] are anagrams of each other, delete words[i].
Return the final words array after no further deletions are possible. The result is the same regardless of operation order.
An anagram rearranges all characters of another word exactly once. For example, "dacb" is an anagram of "abdc".
Example 1:
Input: words = ["abba","baba","bbaa","cd","cd"]
Output: ["abba","cd"]
Explanation:
One of the ways we can obtain the resultant array is by using the following operations:
- Since words[2] = "bbaa" and words[1] = "baba" are anagrams, we choose index 2 and delete words[2].
Now words = ["abba","baba","cd","cd"].
- Since words[1] = "baba" and words[0] = "abba" are anagrams, we choose index 1 and delete words[1].
Now words = ["abba","cd","cd"].
- Since words[2] = "cd" and words[1] = "cd" are anagrams, we choose index 2 and delete words[2].
Now words = ["abba","cd"].
We can no longer perform any operations, so ["abba","cd"] is the final answer.
Example 2:
Input: words = ["a","b","c","d","e"]
Output: ["a","b","c","d","e"]
Explanation:
No two adjacent strings in words are anagrams of each other, so no operations are performed.
Code
1
2
3