LeetCode Entry
2273. Find Resultant Array After Removing Anagrams
Dedup anagrams
2273. Find Resultant Array After Removing Anagrams medium blog post substack youtube

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
}