Find Resultant Array After Removing Anagrams solution leetcode

Find Resultant Array After Removing Anagrams solution leetcode

    • For each violating participant, the first 10 users who submit the violation report towards this participant will each earn 20 LeetCoins.
    • Each user can earn up to 100 LeetCoins for reporting violations in a contest.
    • Users will not be rewarded LeetCoins for reports on LCCN users.

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: You can fill out the contact information at the registration step. LeetCode may reach out to eligible contest winners for an interview opportunity with LeetCode.

⭐ Bonus Prizes⭐

  • Contestants ranked 1st will win a Apple HomePod mini
  • Contestants ranked 2nd will win a Logitech G903 LIGHTSPEED Gaming Mouse
  • Contestants ranked 3rd ~ 5th will win a LeetCode Backpack
  • Contestants ranked 6th ~ 10th will win a LeetCode water bottle
  • Contestants ranked 11th ~ 20th will win a LeetCode Big O Notebook

Only LCUS(leetcode.com) accounts are eligible for the bonus rewards. After the ranking is finalized, a LeetCode team member will reach out to you through email regarding the gift!

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

Find Resultant Array After Removing Anagrams solution leetcode

SOLUTION

Click here

Leave a Comment