Problem List

Find Resultant Array After Removing Anagrams

May 30, 2025Go array, hash table, stringeasy

Problem

Approach

Reflections

This one was easier than I expected, the few number of likes and comments on it made me a bit scared at first. But it's just filtering while comparing to the last accepted word. Honestly, you could also do this with sorting if you wanted to be lazy, sort both strings and compare them.

Performance

Complexity

Go Solution
func removeAnagrams(words []string) []string {
    res := []string{words[0]}
    for _, word := range words {
        if !isAnagram(word, res[len(res) - 1]) {
            res = append(res, word)
        }
    }    

    return res
}

func isAnagram(a,b string) bool {
    if len(a) != len(b) {
        return false 
    }

    m := make(map[rune]int)
    for _,l := range a {
        m[l]++
    }

    for _, l := range b {
        m[l]-- 
        if m[l] < 0 {
            return false
        }
    }

    return true
}
LeetCode Problem Link