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 you can select an index that satisfies the conditions.
Return words after performing 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. 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.
Constraints:
1 <= words.length <= 1001 <= words[i].length <= 10words[i] consists of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach involves going through the list of words one by one. We compare each word to the word right after it to see if they are anagrams of each other. If they are, we remove the second word.
Here's how the algorithm would work step-by-step:
def find_resultant_array_after_removing_anagrams(words):
index = 0
while index < len(words) - 1:
# Compare current word to the next word.
current_word = words[index]
next_word = words[index + 1]
# Check if the current word and the next word are anagrams.
if sorted(current_word) == sorted(next_word):
# Remove the next word if it is an anagram.
words.pop(index + 1)
# Move to the next word if they are not anagrams.
else:
index += 1
return wordsThe core idea is to go through the list of words one by one, checking if consecutive words are anagrams. If two adjacent words are anagrams, we remove the second word. This continues until no adjacent anagrams remain, resulting in the final list.
Here's how the algorithm would work step-by-step:
def find_resultant_array_after_removing_anagrams(words):
while True:
removed_anagram = False
index = 0
while index < len(words) - 1:
# Anagram check ensures order doesn't matter.
if sorted(words[index]) == sorted(words[index + 1]):
words.pop(index + 1)
removed_anagram = True
# Reset index to recheck after removal.
index = 0
else:
index += 1
# If no anagrams were removed, the loop can terminate.
if not removed_anagram:
break
return words| Case | How to Handle |
|---|---|
| Empty input list | Return an empty list immediately as there are no elements to process. |
| List with a single string | Return the original list as a single string cannot form an anagram pair. |
| List with two anagrams | The algorithm should correctly identify and remove one of the anagrams, resulting in a list with only one element. |
| List with consecutive anagrams (e.g., ['abc', 'cba', 'bac']) | The solution should remove 'cba' and 'bac' leaving just 'abc'. |
| List with non-anagram strings | The solution should retain all strings that are not anagrams of their adjacent element. |
| List with strings of varying lengths, including very long strings | Ensure that the anagram comparison method (e.g., sorting) is efficient enough to handle long strings, and consider potential performance bottlenecks. |
| List with Unicode strings | Ensure the anagram detection works correctly with Unicode characters, considering encoding and character representation. |
| List with strings containing spaces or special characters | Consider whether spaces and special characters should be considered when determining if two strings are anagrams, and handle accordingly. |