Find Resultant Array After Removing Anagrams LeetCode Solution

Problem – Find Resultant Array After Removing Anagrams LeetCode Solution

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

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

vector<string> removeAnagrams(vector<string>& A) {
    vector<string> ans;
    int n= size(A);
    for(int i=0;i<n;){
        int j=i+1;
        while( j<n and isang(A[i],A[j]) ) j++;
        ans.push_back(A[i]);
        i=j;
    }
    return ans;
}
//function for checking if two string are anagrams or not
bool isang(string p,string q){
    vector<int> cnt(26);
    int res=0;
    for(auto i:p) cnt[i-'a']++;
    for(auto i:q) cnt[i-'a']--;         
    for(auto i:cnt) if(i!=0) return false;
    return true;
}

Find Resultant Array After Removing Anagrams LeetCode Solution in Java

public List<String> removeAnagrams(String[] A) {
    List<String> ans= new ArrayList<>();
    int n= A.length;
    for(int i=0;i<n;){
        int j=i+1;
        while( j<n && isang(A[i],A[j]) ) j++;
        ans.add(A[i]);
        i=j;
    }
    return ans;
}
//function for checking if two string are anagrams or not
boolean isang(String p,String q){
    int[] cnt= new int[26];
    int res=0;
    for(int i=0;i<p.length();i++) cnt[p.charAt(i)-'a']++;
    for(int i=0;i<q.length();i++) cnt[q.charAt(i)-'a']--;
    for(int i:cnt) if(i!=0) return false;
    return true;
}

Find Resultant Array After Removing Anagrams LeetCode Solution in Python

class Solution:
    def removeAnagrams(self, words: List[str]) -> List[str]:
        if len(words)==1:
            return words
        i = 1 
        while i<len(words):
            if sorted(list(words[i]))==sorted(list(words[i-1])):
                words.pop(i)
            else:
                i+=1 
        return words
Find Resultant Array After Removing Anagrams LeetCode Solution Review:

In our experience, we suggest you solve this Find Resultant Array After Removing Anagrams and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Find Resultant Array After Removing Anagrams LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Find Resultant Array After Removing Anagrams LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.