Using Counter() in Python to find minimum character removal to make two strings anagram Last Updated : 25 Aug, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 Implementation: Python3 # 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)) Output3 Alternate Solution : Convert each string into a dictionary data structure using Counter(iterable) method.Find the common elements from both dictionaryAdd up the values from common dictionary in order to get the total number of common elements. Implementation: Python3 # 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 dictionary 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)) Output3 Time Complexity: O(n) where n is length of string Comment More infoAdvertise with us Next Article Using Counter() in Python to find minimum character removal to make two strings anagram S Shashank Mishra Follow Improve Article Tags : Python anagram Practice Tags : anagrampython Similar Reads Remove minimum characters so that two strings become anagram Given two strings in lowercase, the task is to make them anagrams. The only allowed operation is to remove a character from any string. Find the minimum number of characters to be deleted to make both the strings anagram. Two strings are called anagrams of each other if one of them can be converted 12 min read Python Dictionary to find mirror characters in a string Given a string and a number N, we need to mirror the characters from the N-th position up to the length of the string in alphabetical order. In mirror operation, we change âaâ to âzâ, âbâ to âyâ, and so on. Examples: Input : N = 3 paradox Output : paizwlc We mirror characters from position 3 to end. 2 min read Python code to print common characters of two Strings in alphabetical order Given two strings, print all the common characters in lexicographical order. If there are no common letters, print -1. All letters are lower case. Examples: Input : string1 : geeks string2 : forgeeks Output : eegks Explanation: The letters that are common between the two strings are e(2 times), g(1 2 min read Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character | Set 2 Given two equal-size strings s[] and t[] of size N. In one step, choose any character of t[] and replace it with another character. Return the minimum number of steps to make t[] an anagram of s[]. Note: An Anagram of a string is a string that contains the same characters with a different (or the sa 6 min read Zip function in Python to change to a new character set Given a 26 letter character set, which is equivalent to character set of English alphabet i.e. (abcdâ¦.xyz) and act as a relation. We are also given several sentences and we have to translate them with the help of given new character set. Examples: New character set : qwertyuiopasdfghjklzxcvbnm Input 2 min read Like