LeetCode Entry

2273. Find Resultant Array After Removing Anagrams

13.10.2025 medium 2025 kotlin rust

Dedup anagrams

2273. Find Resultant Array After Removing Anagrams medium blog post substack youtube

ef5404c8-652d-4e6c-ac76-4e95cfe41880 (1).webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1141

Problem TLDR

Dedup anagrams #easy

Intuition

Simulate the process. The islands of equal-by-anagram are not influence each other when split by non-equal word.

Approach

  • going from left to right, take value if previous is not anagram to current
  • check anagrams by: a) sorting b) comparing the frequency map

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(n)\)

Code


// 42ms
    fun removeAnagrams(w: Array<String>) = w.take(1) + w.asList()
    .zipWithNext().mapNotNull {(a,b) -> b.takeIf{a.groupBy{it}!=b.groupBy{it}}}


// 3ms
    pub fn remove_anagrams(mut w: Vec<String>) -> Vec<String> {
       w.dedup_by_key(|w| w.bytes().counts()); w
    }