Problem List
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.
O(n * k) Loop through each word, and for each, we perform a frequency map comparison (O(k) time, k is the max word length).O(k)Each word comparison uses a hash map of size at most O(k) for the 26 lowercase letters.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
}