Remove Anagrams from String Array

Easy
25.0% Acceptance

In this lab, you are given a 0-indexed string array words, where words[i] consists of lowercase English letters. Your task is to write a function removeAnagrams to find the resultant array after removing anagrams from the given string array.

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".

You must create an algorithm, in which you perform an operation to 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.

Example 1:

const words = ["abba","baba","bbaa","cd","cd"]; const result = removeAnagrams(words); console.log(result); // ["abba","cd"]

Example 2:

const words = ["a","b","c","d","e"]; const result = removeAnagrams(words); console.log(result); // ["a","b","c","d","e"]

Constraints:

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 10
  • words[i] consists of lowercase English letters.

Complete the given index.js file by exporting your function as:

/** * @param {string[]} words * @return {string[]} */ export const removeAnagrams = function(words) { };

Note: Make sure to use ESM import/export syntax and use the correct names, otherwise the evaluation script may fail.