Using Counter() in Python 3.x. to find minimum character removal to make two strings anagram

In this article, we will learn how to find the minimum number of character removals needed to make two strings anagrams using Python's Counter() function. Two strings are anagrams when they contain the same characters with the same frequencies, regardless of order.

The Counter() method is available in Python's collections module and provides an efficient way to count character frequencies in strings.

What are Anagrams?

Two strings are anagrams if they contain the same characters with identical frequencies. For example:

  • "listen" and "silent" are anagrams
  • "hello" and "world" are not anagrams

Algorithm

Here's the step-by-step approach to find minimum character removals:

1. Convert both input strings into Counter dictionaries containing 
   character frequencies
2. Find common characters between both strings
3. For each common character, keep the minimum frequency from both strings
4. Calculate total characters to remove from both strings

Implementation Using Counter()

The collections.Counter class automatically counts character frequencies, making anagram detection straightforward:

from collections import Counter

def min_removals_for_anagram(str1, str2):
    # Count character frequencies in both strings
    counter1 = Counter(str1)
    counter2 = Counter(str2)
    
    # Calculate characters to remove from each string
    removals = 0
    
    # Get all unique characters from both strings
    all_chars = set(str1 + str2)
    
    for char in all_chars:
        freq1 = counter1.get(char, 0)
        freq2 = counter2.get(char, 0)
        # Remove excess characters to match minimum frequency
        removals += abs(freq1 - freq2)
    
    return removals // 2  # Each removal affects both strings

# Test examples
str1 = 'Tutorials'
str2 = 'sTutalori'
str3 = 'Point'

print(f"Removals needed for '{str1}' and '{str2}': {min_removals_for_anagram(str1, str2)}")
print(f"Removals needed for '{str3}' and '{str2}': {min_removals_for_anagram(str3, str2)}")
Removals needed for 'Tutorials' and 'sTutalori': 0
Removals needed for 'Point' and 'sTutalori': 8

Alternative Approach

Here's another method that calculates removals by finding the total length minus twice the common character count:

from collections import Counter

def min_removals_alternative(str1, str2):
    counter1 = Counter(str1)
    counter2 = Counter(str2)
    
    # Count characters that can remain (minimum frequency of common chars)
    common_count = 0
    for char in counter1:
        if char in counter2:
            common_count += min(counter1[char], counter2[char])
    
    # Total removals = total length - 2 * common characters
    return len(str1) + len(str2) - 2 * common_count

# Test the alternative approach
str1 = 'abc'
str2 = 'def'
print(f"No common characters: {min_removals_alternative(str1, str2)}")

str1 = 'listen'
str2 = 'silent'
print(f"Perfect anagrams: {min_removals_alternative(str1, str2)}")
No common characters: 6
Perfect anagrams: 0

Comparison Table

String 1 String 2 Common Characters Removals Needed
listen silent All characters 0
abc def None 6
hello bello ello 2

Conclusion

Using Counter() from the collections module provides an efficient way to find minimum character removals for making two strings anagrams. The key is counting character frequencies and calculating the difference between them.

Updated on: 2026-03-25T06:15:42+05:30

264 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements