2273. Find Resultant Array After Removing Anagrams LeetCode Solution

In this guide, you will get 2273. Find Resultant Array After Removing Anagrams LeetCode Solution with the best time and space complexity. The solution to Find Resultant Array After Removing Anagrams problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find Resultant Array After Removing Anagrams solution in C++
  4. Find Resultant Array After Removing Anagrams solution in Java
  5. Find Resultant Array After Removing Anagrams solution in Python
  6. Additional Resources
2273. Find Resultant Array After Removing Anagrams LeetCode Solution image

Problem Statement of 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 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:

See also  1899. Merge Triplets to Form Target Triplet LeetCode Solution

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 <= 100
1 <= words[i].length <= 10
words[i] consists of lowercase English letters.

Complexity Analysis

  • Time Complexity: O(\Sigma |\texttt{words[i]}|)
  • Space Complexity: O(\Sigma |\texttt{words[i]}|)

2273. Find Resultant Array After Removing Anagrams LeetCode Solution in C++

class Solution {
 public:
  vector<string> removeAnagrams(vector<string>& words) {
    vector<string> ans;

    for (int i = 0; i < words.size();) {
      int j = i + 1;
      while (j < words.size() && isAnagram(words[i], words[j]))
        ++j;
      ans.push_back(words[i]);
      i = j;
    }

    return ans;
  }

 private:
  bool isAnagram(const string& a, const string& b) {
    if (a.length() != b.length())
      return false;

    vector<int> count(26);

    for (const char c : a)
      ++count[c - 'a'];

    for (const char c : b)
      --count[c - 'a'];

    return ranges::all_of(count, [](const int c) { return c == 0; });
  }
};
/* code provided by PROGIEZ */

2273. Find Resultant Array After Removing Anagrams LeetCode Solution in Java

class Solution {
  public List<String> removeAnagrams(String[] words) {
    List<String> ans = new ArrayList<>();

    for (int i = 0; i < words.length;) {
      int j = i + 1;
      while (j < words.length && isAnagram(words[i], words[j]))
        ++j;
      ans.add(words[i]);
      i = j;
    }

    return ans;
  }

  private boolean isAnagram(final String a, final String b) {
    if (a.length() != b.length())
      return false;

    int[] count = new int[26];

    for (final char c : a.toCharArray())
      ++count[c - 'a'];

    for (final char c : b.toCharArray())
      --count[c - 'a'];

    return Arrays.stream(count).allMatch(c -> c == 0);
  }
}
// code provided by PROGIEZ

2273. Find Resultant Array After Removing Anagrams LeetCode Solution in Python

class Solution:
  def removeAnagrams(self, words: list[str]) -> list[str]:
    ans = []

    def isAnagram(a: str, b: str) -> bool:
      count = collections.Counter(a)
      count.subtract(collections.Counter(b))
      return all(value == 0 for value in count.values())

    i = 0
    while i < len(words):
      j = i + 1
      while j < len(words) and isAnagram(words[i], words[j]):
        j += 1
      ans.append(words[i])
      i = j

    return ans
# code by PROGIEZ

Additional Resources

See also  3238. Find the Number of Winning Players LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.