# Using Counter() in Python to find minimum character removal to make two strings anagram

Given two strings in lowercase, the task is to make them Anagram. The only allowed operation is to remove a character from any string. Find minimum number of characters to be deleted to make both the strings anagram?

If two strings contains same data set in any order then strings are called **Anagrams**.

Examples:

Input : str1 = "bcadeh" str2 = "hea" Output: 3 We need to remove b, c and d from str1. Input : str1 = "cddgk" str2 = "gcd" Output: 2 Input : str1 = "bca" str2 = "acb" Output: 0

This problem has existing solution please refer Remove minimum number of characters so that two strings become anagram link. We will solve this problem in python quickly using Counter() and Dictionary Data Structure and **intersection** property of Set data structure. Approach is very simple,

- Convert each string into a dictionary data structure using
**Counter(iterable)**method. - Count number of keys in both dictionaries ( count1, count2) and count number of keys common in both dictionaries.
- If no common keys found that means we need to remove
**count1 + count2**characters from both the strings. - Else
**(max(count1, count2) – countCommon)**will be the number of characters to be removed - Convert each string into a dictionary data structure using
**Counter(iterable)**method. - Find the common elements from both dictonary
- Add up the values from common dictionary in order to get the total number of common elements.
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character
- Minimum addition/removal of characters to be done to make frequency of each character prime
- Python Counter to find the size of largest subset of anagram words
- Anagram checking in Python using collections.Counter()
- Minimum removal to make palindrome permutation
- Remove minimum number of characters so that two strings become anagram
- Find the minimum number of preprocess moves required to make two strings equal
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- Number of character corrections in the given strings to make them equal
- Python sorted() to check if two strings are anagram or not
- Minimum operation require to make first and last character same
- Python | Minimum number of subsets with distinct elements using Counter
- Minimum cost to make two strings same
- Minimum Cost To Make Two Strings Identical
- Count of strings that become equal to one of the two strings after one removal
- Minimum characters to be deleted from the end to make given two strings equal
- Minimum Cost to make two Numeric Strings Identical
- Minimum move to end operations to make all strings equal
- Minimum number of pairs required to make two strings same
- Minimum swaps to make two strings equal by swapping only with third string

`# Function remove minimum number of characters so that ` `# two strings become anagram ` `from` `collections ` `import` `Counter ` `def` `removeChars(str1, str2): ` ` ` ` ` `# make dictionaries from both strings ` ` ` `dict1 ` `=` `Counter(str1) ` ` ` `dict2 ` `=` `Counter(str2) ` ` ` ` ` `# extract keys from dict1 and dict2 ` ` ` `keys1 ` `=` `dict1.keys() ` ` ` `keys2 ` `=` `dict2.keys() ` ` ` ` ` `# count number of keys in both lists of keys ` ` ` `count1 ` `=` `len` `(keys1) ` ` ` `count2 ` `=` `len` `(keys2) ` ` ` ` ` `# convert list of keys in set to find common keys ` ` ` `set1 ` `=` `set` `(keys1) ` ` ` `commonKeys ` `=` `len` `(set1.intersection(keys2)) ` ` ` ` ` `if` `(commonKeys ` `=` `=` `0` `): ` ` ` `return` `count1 ` `+` `count2 ` ` ` `else` `: ` ` ` `return` `(` `max` `(count1, count2)` `-` `commonKeys) ` ` ` `# Driver program ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `str1 ` `=` `'bcadeh'` ` ` `str2 ` `=` `'hea'` ` ` `print` `removeChars(str1, str2) ` |

*chevron_right*

*filter_none*

Output:

3

Alternate Solution :

`# Function remove minimum number of characters so that ` `# two strings become anagram ` `from` `collections ` `import` `Counter ` `def` `removeChars(a, b): ` ` ` ` ` `# make dictionaries from both strings ` ` ` `c1 ` `=` `Counter(a) ` ` ` `c2 ` `=` `Counter(b) ` ` ` ` ` `# finding the common elements from both dictonary ` ` ` `common ` `=` `c1&c2 ` ` ` `value ` `=` `0` ` ` ` ` `# adding up the key from common dictionary in order ` ` ` `# to get the total number of common elements ` ` ` `for` `key ` `in` `common: ` ` ` `value ` `=` `value ` `+` `common[key] ` ` ` ` ` `# returning the number of elements to be ` ` ` `# removed to form an anagram ` ` ` `return` `(` `len` `(a)` `-` `2` `*` `value` `+` `len` `(b)) ` ` ` `# Driver program ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `str1 ` `=` `'bcadeh'` ` ` `str2 ` `=` `'hea'` ` ` `print` `removeChars(str1, str2) ` |

*chevron_right*

*filter_none*

Output:

3

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.